最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 日期各種格式之間的相互轉(zhuǎn)換實(shí)例代碼

 更新時(shí)間:2017年02月21日 09:49:32   投稿:lqh  
這篇文章主要介紹了java 日期各種格式之間的相互轉(zhuǎn)換實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

java 日期各種格式之間的相互轉(zhuǎn)換實(shí)例代碼

java日期各種格式之間的相互轉(zhuǎn)換,直接調(diào)用靜態(tài)方法

實(shí)例代碼:

java日期各種格式之間的相互轉(zhuǎn)換,直接調(diào)用靜態(tài)方法


package com.hxhk.cc.util;
 
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import com.lowagie.text.pdf.codec.postscript.ParseException;
 
public class DateUtil {
 
  /**
   * @param args
   * @throws java.text.ParseException 
   * @throws ParseException 
   */
  public static void main(String[] args) throws ParseException, java.text.ParseException {
    DateUtil du = new DateUtil();
    //String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
    long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
    long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
    String date = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
    System.out.println(time);
    System.out.println(time1);
    System.out.println(date);
     
 
 
  }
  // date類(lèi)型轉(zhuǎn)換為String類(lèi)型
   // formatType格式為yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH時(shí)mm分ss秒
   // data Date類(lèi)型的時(shí)間
   public static String dateToString(Date data, String formatType) {
   return new SimpleDateFormat(formatType).format(data);
   }
   
   // long類(lèi)型轉(zhuǎn)換為String類(lèi)型
   // currentTime要轉(zhuǎn)換的long類(lèi)型的時(shí)間
   // formatType要轉(zhuǎn)換的string類(lèi)型的時(shí)間格式
   public static String longToString(long currentTime, String formatType)
   throws ParseException, java.text.ParseException {
   Date date = longToDate(currentTime, formatType); // long類(lèi)型轉(zhuǎn)成Date類(lèi)型
   String strTime = dateToString(date, formatType); // date類(lèi)型轉(zhuǎn)成String
   return strTime;
   }
   
   // string類(lèi)型轉(zhuǎn)換為date類(lèi)型
   // strTime要轉(zhuǎn)換的string類(lèi)型的時(shí)間,formatType要轉(zhuǎn)換的格式y(tǒng)yyy-MM-dd HH:mm:ss//yyyy年MM月dd日
   // HH時(shí)mm分ss秒,
   // strTime的時(shí)間格式必須要與formatType的時(shí)間格式相同
   public static Date stringToDate(String strTime, String formatType)
   throws ParseException, java.text.ParseException {
   SimpleDateFormat formatter = new SimpleDateFormat(formatType);
   Date date = null;
   date = formatter.parse(strTime);
   return date;
   }
   
   // long轉(zhuǎn)換為Date類(lèi)型
   // currentTime要轉(zhuǎn)換的long類(lèi)型的時(shí)間
   // formatType要轉(zhuǎn)換的時(shí)間格式y(tǒng)yyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH時(shí)mm分ss秒
   public static Date longToDate(long currentTime, String formatType)
   throws ParseException, java.text.ParseException {
   Date dateOld = new Date(currentTime); // 根據(jù)long類(lèi)型的毫秒數(shù)生命一個(gè)date類(lèi)型的時(shí)間
   String sDateTime = dateToString(dateOld, formatType); // 把date類(lèi)型的時(shí)間轉(zhuǎn)換為string
   Date date = stringToDate(sDateTime, formatType); // 把String類(lèi)型轉(zhuǎn)換為Date類(lèi)型
   return date;
   }
   
   // string類(lèi)型轉(zhuǎn)換為long類(lèi)型
   // strTime要轉(zhuǎn)換的String類(lèi)型的時(shí)間
   // formatType時(shí)間格式
   // strTime的時(shí)間格式和formatType的時(shí)間格式必須相同
   public static long stringToLong(String strTime, String formatType)
   throws ParseException, java.text.ParseException {
   Date date = stringToDate(strTime, formatType); // String類(lèi)型轉(zhuǎn)成date類(lèi)型
   if (date == null) {
   return 0;
   } else {
   long currentTime = dateToLong(date); // date類(lèi)型轉(zhuǎn)成long類(lèi)型
   return currentTime;
   }
   }
   
   // date類(lèi)型轉(zhuǎn)換為long類(lèi)型
   // date要轉(zhuǎn)換的date類(lèi)型的時(shí)間
   public static long dateToLong(Date date) {
   return date.getTime();
   }
   public static String numToDate(int number,String formatType){
     Date date = new Date(number);
     SimpleDateFormat sdf = new SimpleDateFormat(formatType);
     return sdf.format(date);
   }
 
}

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

富阳市| 馆陶县| 思茅市| 财经| 三亚市| 四会市| 祁东县| 万山特区| 平和县| 方正县| 海丰县| 泽普县| 怀柔区| 中山市| 商水县| 黎城县| 盖州市| 福清市| 新兴县| 卓资县| 公主岭市| 桃源县| 芦山县| 东方市| 柳林县| 丹阳市| 陇南市| 凌云县| 句容市| 故城县| 道真| 济南市| 祁门县| 南江县| 盐山县| 土默特右旗| 商南县| 宁晋县| 阜新| 沅江市| 嘉义市|