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简单类型资源(Bool、Color、Dimension、ID、Integer、Inte
  2. android activity之间传递bean类型数据
  3. Android中系统设置参数改变监听(以时间同步为例)
  4. android 判断 网络 类型
  5. android获取系统当前年月日时分秒的时间
  6. Android中Data和String数据类型转换
  7. Android视频播放项目总结之 使用Android中的videoView自己定义,暂
  8. Android MIME类型与文件后缀名匹配部分

随机推荐

  1. Android全屏Activity的几种方式
  2. Android getResources的作用和需要注意点
  3. 15个Android很有用的代码片段
  4. Android Intent Service Usage
  5. Android tp的虚拟按键(virtual key)处理
  6. Android Debug Bridge命令介绍
  7. android源码编译出现No private recovery
  8. Android系统升级的完整过程
  9. Android中使用log4j
  10. add study URL