十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
利用java如何實(shí)現(xiàn)轉(zhuǎn)換日期格式?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

主要從事網(wǎng)頁設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)、程序開發(fā)、微網(wǎng)站、小程序開發(fā)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。
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類型轉(zhuǎn)換為String類型
// formatType格式為yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH時(shí)mm分ss秒
// data Date類型的時(shí)間
public static String dateToString(Date data, String formatType) {
return new SimpleDateFormat(formatType).format(data);
}
// long類型轉(zhuǎn)換為String類型
// currentTime要轉(zhuǎn)換的long類型的時(shí)間
// formatType要轉(zhuǎn)換的string類型的時(shí)間格式
public static String longToString(long currentTime, String formatType)
throws ParseException, java.text.ParseException {
Date date = longToDate(currentTime, formatType); // long類型轉(zhuǎn)成Date類型
String strTime = dateToString(date, formatType); // date類型轉(zhuǎn)成String
return strTime;
}
// string類型轉(zhuǎn)換為date類型
// strTime要轉(zhuǎn)換的string類型的時(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類型
// currentTime要轉(zhuǎn)換的long類型的時(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類型的毫秒數(shù)生命一個(gè)date類型的時(shí)間
String sDateTime = dateToString(dateOld, formatType); // 把date類型的時(shí)間轉(zhuǎn)換為string
Date date = stringToDate(sDateTime, formatType); // 把String類型轉(zhuǎn)換為Date類型
return date;
}
// string類型轉(zhuǎn)換為long類型
// strTime要轉(zhuǎn)換的String類型的時(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類型轉(zhuǎn)成date類型
if (date == null) {
return 0;
} else {
long currentTime = dateToLong(date); // date類型轉(zhuǎn)成long類型
return currentTime;
}
}
// date類型轉(zhuǎn)換為long類型
// date要轉(zhuǎn)換的date類型的時(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);
}
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。