date类型转换为String类型

// formatType 格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒//或者自定义格式    // data Date类型的时间    public static String dateToString(Date data, String formatType) {        return new SimpleDateFormat(formatType).format(data);    }

string类型转换为date类型

注:strTime的时间格式必须要与formatType的时间格式相同

// strTime 要转换的string类型的时间,// formatType 要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日HH时mm分ss秒,    // strTime的时间格式必须要与formatType的时间格式相同    public static Date stringToDate(String strTime, String formatType)            throws ParseException {        SimpleDateFormat formatter = new SimpleDateFormat(formatType);        Date date = null;        date = formatter.parse(strTime);        return date;    }

long转换为Date类型

// currentTime 要转换的long类型的时间 // formatType 要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 public static Date longToDate(long currentTime, String formatType) throws ParseException { Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型  return date; }

date类型转换为long类型

// date 要转换的date类型的时间    public static long dateToLong(Date date) {        return date.getTime();    }

long类型转换为String类型

// currentTime 要转换的long类型的时间    // formatType 要转换的string类型的时间格式    public static String longToString(long currentTime, String formatType)            throws ParseException {        Date date = longToDate(currentTime, formatType); // long类型转成Date类型        String strTime = dateToString(date, formatType); // date类型转成String        return strTime;    }

String类型转换为long类型
注:strTime的时间格式和formatType的时间格式必须相同

// strTime 要转换的String类型的时间    // formatType 时间格式    // strTime的时间格式和formatType的时间格式必须相同    public static long stringToLong(String strTime, String formatType)            throws ParseException {        Date date = stringToDate(strTime, formatType); // String类型转成date类型        if (date == null) {            return 0;        } else {            long currentTime = dateToLong(date); // date类型转成long类型            return currentTime;        }    }

更多相关文章

  1. Android连接指定的wifi热点
  2. android 开发 时间选择器TimePicker的使用
  3. AndroidのCountDownTimer倒计时器
  4. Android设置角标提示
  5. android 打开文件
  6. Android简单类型资源(Bool、Color、Dimension、ID、Integer、Inte
  7. Android中系统设置参数改变监听(以时间同步为例)
  8. Android(安卓)adb命令启动系统组件
  9. Android逆向基础之Dalvik指令集

随机推荐

  1. android手机两种方式获取IP地址
  2. Android(安卓)SDK下载和更新失败的解决方
  3. AndroidのUI布局之layout weight
  4. android debug
  5. include merge的使用
  6. android 设置时区
  7. Android:android studio提示adb: ADB ser
  8. 2011.08.12(2)——— android MediaPlayer
  9. Android下检测网络连接 3G WIFI
  10. android卡片布局CardView