[Java] Java日期格式之间的相互转换代码 →→→→→进入此内容的聊天室

来自 , 2021-01-22, 写在 Java, 查看 154 次.
URL http://www.code666.cn/view/ef825673
  1. package com.hxhk.cc.util;
  2.  
  3.  
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7. import com.lowagie.text.pdf.codec.postscript.ParseException;
  8.  
  9. public class DateUtil {
  10.  
  11.         /**
  12.          * @param args
  13.          * @throws java.text.ParseException
  14.          * @throws ParseException
  15.          */
  16.         public static void main(String[] args) throws ParseException, java.text.ParseException {
  17.                 DateUtil du = new DateUtil();
  18.                 //String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
  19.                 long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
  20.                 long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
  21.                 String date = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
  22.                 System.out.println(time);
  23.                 System.out.println(time1);
  24.                 System.out.println(date);
  25.                
  26.  
  27.  
  28.         }
  29.     // date类型转换为String类型
  30.           // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
  31.           // data Date类型的时间
  32.           public static String dateToString(Date data, String formatType) {
  33.           return new SimpleDateFormat(formatType).format(data);
  34.           }
  35.          
  36.           // long类型转换为String类型
  37.           // currentTime要转换的long类型的时间
  38.           // formatType要转换的string类型的时间格式
  39.           public static String longToString(long currentTime, String formatType)
  40.           throws ParseException, java.text.ParseException {
  41.           Date date = longToDate(currentTime, formatType); // long类型转成Date类型
  42.           String strTime = dateToString(date, formatType); // date类型转成String
  43.           return strTime;
  44.           }
  45.          
  46.           // string类型转换为date类型
  47.           // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
  48.           // HH时mm分ss秒,
  49.           // strTime的时间格式必须要与formatType的时间格式相同
  50.           public static Date stringToDate(String strTime, String formatType)
  51.           throws ParseException, java.text.ParseException {
  52.           SimpleDateFormat formatter = new SimpleDateFormat(formatType);
  53.           Date date = null;
  54.           date = formatter.parse(strTime);
  55.           return date;
  56.           }
  57.          
  58.           // long转换为Date类型
  59.           // currentTime要转换的long类型的时间
  60.           // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
  61.           public static Date longToDate(long currentTime, String formatType)
  62.           throws ParseException, java.text.ParseException {
  63.           Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
  64.           String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
  65.           Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
  66.           return date;
  67.           }
  68.          
  69.           // string类型转换为long类型
  70.           // strTime要转换的String类型的时间
  71.           // formatType时间格式
  72.           // strTime的时间格式和formatType的时间格式必须相同
  73.           public static long stringToLong(String strTime, String formatType)
  74.           throws ParseException, java.text.ParseException {
  75.           Date date = stringToDate(strTime, formatType); // String类型转成date类型
  76.           if (date == null) {
  77.           return 0;
  78.           } else {
  79.           long currentTime = dateToLong(date); // date类型转成long类型
  80.           return currentTime;
  81.           }
  82.           }
  83.          
  84.           // date类型转换为long类型
  85.           // date要转换的date类型的时间
  86.           public static long dateToLong(Date date) {
  87.           return date.getTime();
  88.           }
  89.           public static String numToDate(int number,String formatType){
  90.                   Date date = new Date(number);
  91.                   SimpleDateFormat sdf = new SimpleDateFormat(formatType);
  92.                   return sdf.format(date);
  93.           }
  94.  
  95. }
  96.  
  97. //java/6182

回复 "Java日期格式之间的相互转换代码"

这儿你可以回复上面这条便签

captcha