溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java常用時(shí)間格式轉(zhuǎn)換工具類有哪些

發(fā)布時(shí)間:2021-11-20 13:59:47 來源:億速云 閱讀:141 作者:小新 欄目:編程語言

小編給大家分享一下Java常用時(shí)間格式轉(zhuǎn)換工具類有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

  開發(fā)過程中,經(jīng)常遇到各種時(shí)間格式的轉(zhuǎn)換。今天特此以博客的方式,記錄開發(fā)過程中可能遇到的各種類型時(shí)間的格式轉(zhuǎn)換,以工具類的方式引入,方便大家開發(fā)中使用!

  public final class DataUtils{

  /**

  * 預(yù)設(shè)不同的時(shí)間格式

  */

  //精確到年月日(英文) eg:2019-11-11

  public static String FORMAT_LONOGRAM = "yyyy-MM-dd" ;

  //精確到時(shí)分秒的完整時(shí)間(英文) eg:2010-11-11 12:12:12

  public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";

  //精確到毫秒完整時(shí)間(英文) eg:2019-11-11 12:12:12.55

  public static String FORMAT_FULL_MILL = "yyyy-MM-dd HH:mm:ss.SSS"

  //精確到年月日(中文)eg:2019年11月11日

  public static String FORMAT_LONOGRAM_CN = "yyyy年MM月dd日"

  //精確到時(shí)分秒的完整時(shí)間(中文)eg:2019年11月11日 12時(shí)12分12秒

  public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH時(shí)mm分ss秒"

  //精確到毫秒完整時(shí)間(中文)

  public static String FORMAT_FULL_MILL_CN = "yyyy年MM月dd日 HH時(shí)mm分ss秒SSS毫秒"

  /**

  *預(yù)設(shè)默認(rèn)的時(shí)間格式

  */

  public static String getDefaultFormat(){return FORMAT_FULL};

  /**

  * 預(yù)設(shè)格式格式化日期

  */

  public static String format(Date date) {

  return format(date, getDefaultFormat());

  }

  /**

  *自定義格式格式化日期

  */

  public static String format(Date date,String format){

  String value = "";

  if(date!=null){

  SimpleDateFormat sdf = new SimpleDateFormat(format);

  value = sdf.format(date);

  }

  return value;

  }

  /**

  *根據(jù)預(yù)設(shè)默認(rèn)格式,返回當(dāng)前日期

  */

  public static String getNow(){return format(new Date());}

  /**

  *自定義時(shí)間格式,返回當(dāng)前日期

  */

  public static String getNow(String format){return format(new Date(),format);}

  /**

  *根據(jù)預(yù)設(shè)默認(rèn)時(shí)間 String->Date

  */

  public static Date parse(String strDate){return parse(strDate,getDefaultFormat)};

  /**

  *自定義時(shí)間格式:Stirng->Date

  */

  public static Date parse(String strDate,String format){

  SimpleDateFormat sdf = new SimpleDateFormat(format);

  try{

  return sdf.parse(strDate);

  }catch(ParseException e){

  e.printStackTrace();

  return null;

  }

  }

  /**

  *基于指定日期增加年

  *@param num 整數(shù)往后推,負(fù)數(shù)往前移

  */

  public static Date addMobth(Date date,int num){

  Calendar= Calendar.getInstance();

  cal.setTime(date);

  cal.add(Calendar.YEAR,num);

  return cal.getTime();

  }

  /**

  *基于指定日期增加整月

  *@param num 整數(shù)往后推,負(fù)數(shù)往前移

  */

  public static Date addMobth(Date date,int num){

  Calendar= Calendar.getInstance();

  cal.setTime(date);

  cal.add(Calendar.MONTH,num);

  return cal.getTime();

  }

  /**

  *基于指定日期增加天數(shù)

  *@param num 整數(shù)往后推,負(fù)數(shù)往前移

  */

  public static Date addDay(Date date, int num) {

  Calendar cal = Calendar.getInstance();

  cal.setTime(date);

  cal.add(Calendar.DATE, num);

  return cal.getTime();

  }

  /**

  *基于指定日期增加分鐘

  *@param num 整數(shù)往后推,負(fù)數(shù)往前移

  */

  public static Date addMinute(Date date, int num) {

  Calendar cal = Calendar.getInstance();

  cal.setTime(date);

  cal.add(Calendar.MINUTE, num);

  return cal.getTime();

  }

  /**

  *獲取時(shí)間戳 eg:yyyy-MM-dd HH:mm:ss.S

  */

  public static String getTimeStamp(){

  SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_FULL_MILL);

  Calendar cal = Calendar.getInstance();

  return sdf.format(Calendar.getTime());

  }

  /**

  *獲取日期年份

  */

  public static String getYear(Date date){return format(date).substring(0,4);}

  /**

  *獲取年份+月

  */

  public static String getYearMonth(Date date){return format(date).substring(0,7);}

  /**

  *獲取日期的小時(shí)數(shù)

  */

  public static int getHour(Date date){

  Calendar calendar = Calendar.getInstrance();

  calendar.setTime(date);

  return calendar.get(Calendar.HOUR_OFDAY);

  }

  /**

  *自定義時(shí)間格式字符串距離今天的天數(shù)

  */

  public static int countDays(String strDate,String format){

  long time = Calendar.getInstance.getTime().getTime();

  Calendar cal = Calendar.getInstance();

  cal.setTime(parse(strDate,format));

  long diff = cal.getTime().getTime();

  return (int) (time/1000 - diff/1000)/3600/24;

  }

  /**

  * 預(yù)設(shè)格式的字符串距離今天的天數(shù)

  */

  public static int countDays(String strDate){return countDays(strDate,getDefaultFormat());}

  /**

  * 獲取天數(shù)差值(依賴時(shí)間)

  * @return

  */

  public static int diffDays(Date date1, Date date2) {

  if (date1 == null || date2 == null) return 0;

  return (int) (Math.abs(date1.getTime() - date2.getTime()) / (60 * 60 * 24 * 1000));

  }鄭州人流醫(yī)院哪家好 http://mobile.zyyyzz.com/

  /**

  *獲取年份差值

  */

  public static int diffYear(Date year1,Date year2){return diffDays(year1,year2) / 365;}

  /**

  * 獲取天數(shù)差值(依賴Date類型的日期)

  * @return

  */

  public static int diffByDays(Date d1, Date d2) {

  Date s1 = parse(format(d1, FORMAT_LONOGRAM ), FORMAT_LONOGRAM );

  Date s2 = parse(format(d2, FORMAT_LONOGRAM ), FORMAT_LONOGRAM );

  return diffDays(s1, s2);

  }

  /**

  * 獲取時(shí)間分割集合

  *

  * @param date 查詢?nèi)掌?/p>

  * @param strs 帶拆分的時(shí)間點(diǎn)

  * @return

  */

  public static List collectTimes(Date date, String[] strs) {

  List result = new ArrayList<>();

  List times = Arrays.asList(strs);

  String dateStr = format(date, FORMAT_LONOGRAM );

  String pattern = FORMAT_LONOGRAM + " k";

  if (times.size() > 0) {

  times.stream().forEach(t -> {

  result.add(parse(dateStr + " " + t, pattern));

  });

  }

  return result;

  }

  /**

  * 根據(jù)日期查詢當(dāng)前為周幾

  * @param dt

  * @return

  */

  public static String getWeekOfDate(Date dt) {

  String[] weekDays = {"1", "2", "3", "4", "5", "6", "7"};

  Calendar cal = Calendar.getInstance();

  cal.setTime(dt);

  int w = cal.get(Calendar.DAY_OF_WEEK);

  if (0 == w) {

  w = 7;

  }

  return weekDays[w];

  }

  public static String intToCn(int hourInt, String[] timeArray) {

  String result = "";

  if (0 <= hourInt && hourInt <= 10) {

  result += timeArray[hourInt] + "\n";

  } else if (11 <= hourInt && hourInt <= 19) {

  result += (timeArray[10] + "\n" + timeArray[hourInt % 10]) + "\n";

  } else {

  result += (timeArray[hourInt / 10] + "\n" + timeArray[10] + "\n" + (hourInt % 10 == 0 ? "" : timeArray[hourInt % 10] + "\n"));

  }

  return result;

  }

  /**

  * 將時(shí)間轉(zhuǎn)換成漢字

  * @param hour

  * @return

  */

  public static String hourToCn(String hour){

  String[] timeArray = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};

  String[] hourArray = hour.split(":");

  int hourInt = Integer.parseInt(hourArray[0]);

  int minute = Integer.parseInt(hourArray[1]);

  String result = intToZh(hourInt, timeArray) + "點(diǎn)\n" + intToZh(minute, timeArray) + "分";

  return result;

  }

  /**

  * 獲取當(dāng)前日期后的一周時(shí)間,并返回LinkedHashMap

  * @param startTime

  * @return

  */

  public static LinkedHashMap dateAfterWeek(String startTime) {

  LinkedHashMap result = new LinkedHashMap<>();

  try {

  Date date = parse(startTime,FORMAT_LONOGRAM);

  for (int i = 0; i < 7; i++) {

  Calendar calendar = Calendar.getInstance();

  calendar.setTime(date);

  calendar.add(calendar.DATE, m); //把日期往后增加一天,整數(shù)往后推,負(fù)數(shù)往前移動(dòng) 時(shí)間戳轉(zhuǎn)時(shí)間

  Date newDate = calendar.getTime();

  String str = new SimpleDateFormat("yyyy-MM-dd").format(newDate);

  result.put(str, newDate);

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  return result;

  }

  /**

  * 獲取當(dāng)前日期 后的一周時(shí)間,并返回yyyy-MM-dd字符串?dāng)?shù)組

  * @param startTime

  * @return

  */

  public static String[] dateAfterWeekArray(String startTime) {

  String weekArray[] = new String[7];

  try {

  Date date = parse(startTime,FORMAT_LONOGRAM);

  for (int i = 0; i < 7; i++) {

  Calendar calendar = Calendar.getInstance();

  calendar.setTime(date);

  calendar.add(calendar.DATE, m); //把日期往后增加一天,整數(shù)往后推,負(fù)數(shù)往前移動(dòng) 時(shí)間戳轉(zhuǎn)時(shí)間

  Date newDate = calendar.getTime();

  weekArray[m] = new SimpleDateFormat("yyyy-MM-dd").format(newDate);

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  return weekArray;

  }

  }

以上是“Java常用時(shí)間格式轉(zhuǎn)換工具類有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI