java Data、String、Long三种日期类型之间的相互转换

  1. // date类型转换为String类型  
  2.     // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒  
  3.     // data Date类型的时间  
  4.     public static String dateToString(Date data, String formatType) {  
  5.         return new SimpleDateFormat(formatType).format(data);  
  6.     }  
  7.    
  8.     // long类型转换为String类型  
  9.     // currentTime要转换的long类型的时间  
  10.     // formatType要转换的string类型的时间格式  
  11.     public static String longToString(long currentTime, String formatType)  
  12.             throws ParseException {  
  13.         Date date = longToDate(currentTime, formatType); // long类型转成Date类型  
  14.         String strTime = dateToString(date, formatType); // date类型转成String  
  15.         return strTime;  
  16.     }  
  17.    
  18.     // string类型转换为date类型  
  19.     // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日  
  20.     // HH时mm分ss秒,  
  21.     // strTime的时间格式必须要与formatType的时间格式相同  
  22.     public static Date stringToDate(String strTime, String formatType)  
  23.             throws ParseException {  
  24.         SimpleDateFormat formatter = new SimpleDateFormat(formatType);  
  25.         Date date = null;  
  26.         date = formatter.parse(strTime);  
  27.         return date;  
  28.     }  
  29.    
  30.     // long转换为Date类型  
  31.     // currentTime要转换的long类型的时间  
  32.     // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒  
  33.     public static Date longToDate(long currentTime, String formatType)  
  34.             throws ParseException {  
  35.         Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间  
  36.         String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string  
  37.         Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型  
  38.         return date;  
  39.     }  
  40.    
  41.     // string类型转换为long类型  
  42.     // strTime要转换的String类型的时间  
  43.     // formatType时间格式  
  44.     // strTime的时间格式和formatType的时间格式必须相同  
  45.     public static long stringToLong(String strTime, String formatType)  
  46.             throws ParseException {  
  47.         Date date = stringToDate(strTime, formatType); // String类型转成date类型  
  48.         if (date == null) {  
  49.             return 0;  
  50.         } else {  
  51.             long currentTime = dateToLong(date); // date类型转成long类型  
  52.             return currentTime;  
  53.         }  
  54.     }  
  55.    
  56.     // date类型转换为long类型  
  57.     // date要转换的date类型的时间  
  58.     public static long dateToLong(Date date) {  
  59.         return date.getTime();  
  60.     } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值