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

java時間戳與日期相互轉(zhuǎn)換工具詳解

 更新時間:2017年12月08日 11:58:25   作者:哈爾濱食尸鬼  
這篇文章主要為大家詳細(xì)介紹了java各種時間戳與日期之間相互轉(zhuǎn)換的工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了java日期與時間戳相互轉(zhuǎn)換大全,供大家參考,具體內(nèi)容如下

package com.crm.util; 
 
import java.math.BigDecimal; 
import java.text.DecimalFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
 
 
/** 
 * @author DingJiaCheng 
 * */ 
public class DateFormatUtil { 
   
  /** 
   * 時間戳轉(zhuǎn)日期 
   * @param ms 
   * @return 
   */ 
  public static Date transForDate(Integer ms){ 
    if(ms==null){ 
      ms=0; 
    } 
    long msl=(long)ms*1000; 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date temp=null; 
    if(ms!=null){ 
      try { 
        String str=sdf.format(msl); 
        temp=sdf.parse(str); 
      } catch (ParseException e) { 
        e.printStackTrace(); 
      } 
    } 
    return temp; 
  } 
   
  /** 
   * 獲取晚上9點(diǎn)半的時間戳 
   * 
   * @return 
   */ 
  public static int getTimes(int day, int hour, int minute) { 
    Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.DATE, day); 
    cal.set(Calendar.HOUR_OF_DAY, hour); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MINUTE, minute); 
    cal.set(Calendar.MILLISECOND, 0); 
    return (int) (cal.getTimeInMillis() / 1000); 
  } 
   
  /** 
   * 獲取當(dāng)前時間往上的整點(diǎn)時間 
   * 
   * @return 
   */ 
  public static int getIntegralTime() { 
    Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.HOUR_OF_DAY, 1); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.MILLISECOND, 0); 
    return (int) (cal.getTimeInMillis() / 1000); 
  } 
   
  public static int getIntegralTimeEnd() { 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, 24); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.MILLISECOND, 0); 
    return (int) (cal.getTimeInMillis() / 1000); 
  } 
   
  /** 
   * 時間戳轉(zhuǎn)日期 
   * @param ms 
   * @return 
   */ 
  public static Date transForDate3(Integer ms){ 
    if(ms==null){ 
      ms=0; 
    } 
    long msl=(long)ms*1000; 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
    Date temp=null; 
    if(ms!=null){ 
      try { 
        String str=sdf.format(msl); 
        temp=sdf.parse(str); 
      } catch (ParseException e) { 
        e.printStackTrace(); 
      } 
    } 
    return temp; 
  } 
  /** 
   * 時間戳轉(zhuǎn)日期 
   * @param ms 
   * @return 
   */ 
  public static Date transForDate(Long ms){ 
    if(ms==null){ 
      ms=(long)0; 
    } 
    long msl=(long)ms*1000; 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date temp=null; 
    if(ms!=null){ 
      try { 
        String str=sdf.format(msl); 
        temp=sdf.parse(str); 
      } catch (ParseException e) { 
        e.printStackTrace(); 
      } 
    } 
    return temp; 
  } 
   
   
  public static String transForDate1(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    }     
    return str; 
  } 
  public static String transForDate2(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
  public static String transForDate4(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
   
  public static String transForDate5(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
  public static String transForDateInChinese(Integer ms){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); 
       
      if(ms!=null){ 
        try { 
          str=sdf.format(msl); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    }     
    return str; 
  } 
   
  /** 
   * 日期轉(zhuǎn)時間戳 
   * @param date 
   * @return 
   */ 
  public static Integer transForMilliSecond(Date date){ 
    if(date==null) return null; 
    return (int)(date.getTime()/1000); 
  } 
   
  /** 
   * 獲取當(dāng)前時間戳 
   * @return 
   */ 
  public static Integer currentTimeStamp(){ 
    return (int)(System.currentTimeMillis()/1000); 
  } 
   
  /** 
   * 日期字符串轉(zhuǎn)時間戳 
   * @param dateStr 
   * @return 
   */ 
  public static Integer transForMilliSecond(String dateStr){ 
    Date date = DateFormatUtil.formatDate(dateStr); 
    return date == null ? null : DateFormatUtil.transForMilliSecond(date); 
  } 
  /** 
   * 日期字符串轉(zhuǎn)時間戳 
   * @param dateStr 
   * @return 
   */ 
  public static Integer transForMilliSecond(String dateStr,String format){ 
    Date date = DateFormatUtil.formatDate(dateStr,format); 
    return date == null ? null : DateFormatUtil.transForMilliSecond(date); 
  } 
  /** 
   * 日期字符串轉(zhuǎn)時間戳 
   * @param dateStr 
   * @param 格式 如"yyyy-mm-dd" 
   * @return 
   */ 
  public static Integer transForMilliSecondByTim(String dateStr,String tim){ 
    SimpleDateFormat sdf=new SimpleDateFormat(tim); 
    Date date =null; 
    try { 
      date = sdf.parse(dateStr); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return date == null ? null : DateFormatUtil.transForMilliSecond(date); 
  } 
  /** 
   * 字符串轉(zhuǎn)日期,格式為:"yyyy-MM-dd HH:mm:ss" 
   * @param dateStr 
   * @return 
   */ 
  public static Date formatDate(String dateStr){ 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date result=null; 
    try { 
      result = sdf.parse(dateStr); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return result; 
  } 
  /** 
   * 字符串轉(zhuǎn)日期,格式為:"yyyy-MM-dd HH:mm:ss" 
   * @param dateStr 
   * @return 
   */ 
  public static Date formatDate(String dateStr,String format){ 
    SimpleDateFormat sdf=new SimpleDateFormat(format); 
    Date result=null; 
    try { 
      result = sdf.parse(dateStr); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return result; 
  } 
  /** 
   * 日期轉(zhuǎn)字符串 
   * @param date 
   * @return 
   */ 
  public static String formatDate(Date date){ 
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String result=null; 
    result = sdf.format(date); 
    return result; 
  } 
  /** 
   * 日期轉(zhuǎn)字符串 
   * @param date 
   * @return 
   */ 
  public static String formatDate(Date date,String format){ 
    SimpleDateFormat sdf=new SimpleDateFormat(format); 
    String result=null; 
    result = sdf.format(date); 
    return result; 
  } 
  /** 
   * 時間戳格式化輸出(httl模版用) 
   * 
   * @param ms    時間戳 
   * @param format  格式化 
   * @return 
   */ 
  public static String transForDate(Integer ms, String format){ 
    String str = ""; 
    if(ms!=null){ 
      long msl=(long)ms*1000; 
      SimpleDateFormat sdf=new SimpleDateFormat(format); 
      if(!ms.equals(0)){ 
        try { 
          str=sdf.format(msl); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
    } 
    return str; 
  }   
   
  /** 
   * 取BigDecimal類型數(shù)的整數(shù)或小數(shù)部分(httl模版用) 
   * 
   * @param b 值 
   * @param mode 模式 0取整 1去小數(shù)部分 
   * @return 
   */ 
  public static String splitBigDecimal(BigDecimal b, int mode) { 
    DecimalFormat df = new DecimalFormat("0.00"); 
    String s = df.format(b); 
    if(mode==0){ 
      return s.split("\\.")[0]; 
    }else { 
      return "."+s.split("\\.")[1]; 
    } 
  } 
   
  /** 
   * 計算兩個日期之間差的天數(shù)(httl模版用) 
   * 
   * @param ts1  時間戳1 
   * @param ts2  時間戳2 
   * @return 
   */ 
  public static int caculate2Days(Integer ts1, Integer ts2) { 
    Date firstDate = DateFormatUtil.transForDate(ts1); 
    Date secondDate = DateFormatUtil.transForDate(ts2); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(firstDate); 
    int dayNum1 = calendar.get(Calendar.DAY_OF_YEAR); 
    calendar.setTime(secondDate); 
    int dayNum2 = calendar.get(Calendar.DAY_OF_YEAR); 
    return Math.abs(dayNum1 - dayNum2); 
  } 
   
  /** 
   * 給手機(jī)加密中間四位加星號 
   * 
   * @param mobile 
   * @return 
   */ 
  public String mobileSerect(String mobile){ 
    if(!StringUtils.isBlank(mobile)){ 
      int between = mobile.length()/2; 
      mobile = mobile.substring(0, between-2)+"****"+mobile.substring(between+2, mobile.length()); 
    } 
    return mobile; 
  } 
   
  /** 
   * 給郵箱加密加星號 
   * 
   * @param email 
   * @return 
   */ 
  public String emailSerect(String email) { 
    if(!StringUtils.isBlank(email)){ 
      int length = email.lastIndexOf("@"); 
      email = email.substring(0, 2)+"****"+email.substring(length-2, email.length()); 
    } 
    return email; 
  } 
   
  /** 
   * BigDecimal類型數(shù)據(jù)相加 
   * 
   * @param BigDecimal source 
   * @param BigDecimal target 
   * @return 
   */ 
  public BigDecimal sumBigDicimal(BigDecimal source, BigDecimal target) { 
    source = source.add(target); 
    return source; 
  } 
   
  /** 
   * BigDecimal類型數(shù)據(jù)相加 
   * 
   * @param BigDecimal source 
   * @param BigDecimal target 
   * @return 
   */ 
  public BigDecimal sumBigDicimalAndDouble(BigDecimal source, Double target) { 
    BigDecimal new_target = new BigDecimal(target); 
    source = source.add(new_target); 
    return source; 
  } 
   
  /** 
   * BigDecimal類型數(shù)據(jù)相減 
   * 
   * @param BigDecimal source 
   * @param BigDecimal target 
   * @return 
   */ 
  public BigDecimal subBigDicimal(BigDecimal source, BigDecimal target) { 
    source = source.subtract(target); 
    return source; 
  } 
   
  /** 
   * 獲取傳入時間和當(dāng)前時間的時間差 
   * @return 
   */ 
  public static Long getTimediff(int timeStamp){ 
    Date d1 = DateFormatUtil.transForDate(timeStamp); 
    Date today = new Date(); 
    if(d1.getTime()<today.getTime()){ 
      return null; 
    } 
    return (d1.getTime()-today.getTime())/1000; 
  } 
 
  /** 
   * 獲取某周的第一天日期 
   * @param week 0 當(dāng)周 1 上一周 -1 下一周 
   * @return 
   */ 
  public static String weekFirstDay(int week){ 
    Calendar c1 = Calendar.getInstance(); 
    int dow = c1.get(Calendar.DAY_OF_WEEK); 
    c1.add(Calendar.DATE, -dow-7*(week-1)-5 ); 
    String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime()); 
    return d1+" 00:00:00"; 
  } 
   
  /** 
   * 當(dāng)前時間加一年 
   */ 
  public static String addYear(int startTime){ 
    Date firstDate = DateFormatUtil.transForDate(startTime); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(firstDate); 
    calendar.add(Calendar.YEAR,1); 
    String d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); 
    return d1; 
  } 
   
  /** 
   * 獲取某周的最后一天日期 
   * @param week 
   * @return 
   */ 
  public static String weekLastDay(int week){ 
    Calendar c1 = Calendar.getInstance(); 
    int dow = c1.get(Calendar.DAY_OF_WEEK); 
    c1.add(Calendar.DATE, -dow-7*(week-1)+1); 
    String d1 = new SimpleDateFormat("yyyy-MM-dd").format(c1.getTime()); 
    return d1+" 23:59:59"; 
  }   
   
  /** 
   * 和當(dāng)前時間比對 
   * @return 
   */ 
  public static boolean greaterThanNow(int timeStamp){ 
    Date d1 = DateFormatUtil.transForDate(timeStamp); 
    Date today = new Date(); 
    if(d1.getTime()>=today.getTime()){ 
      return true; 
    } 
    return false; 
  } 
 
   
   
  /** 
   * HH:mm:ss格式時間轉(zhuǎn)換為1970-01-01日的時間戳(也就是只有時間沒有日期的情況要求使用時間戳表示時間) 
   * @author DingJiaCheng 
   * */ 
  public static int transFromTime(String time){ 
    return transForMilliSecond("1970-01-01 "+time,"yyyy-mm-dd HH:mm:ss"); 
  } 
   
  /** 
   * 時間戳轉(zhuǎn)換為HH:mm:ss格式時間(日期去除) 
   * @author DingJiaCheng 
   * */ 
  public static String transToTime(int time){ 
    String s = new String(transForDate1(time)); 
    String ss[] = s.split(" "); 
    return ss[1]; 
  } 
   
  public static int transToChuo(String dateString){ 
    SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd"); 
    int res = 0; 
    try { 
      Date date=simpleDateFormat .parse(dateString); 
      res = (int) date.getTime(); 
    } catch (ParseException e) { 
      e.printStackTrace(); 
    } 
    return res; 
  } 
   
  public static void main(String[] args) { 
     
    //System.out.println(getIntegralTimeEnd()); 
    System.out.println(transForDate2(transForMilliSecond("2015-02-25 00:00:00"))); 
    //System.out.println(transForMilliSecond("2016-01-25","yyyy-mm-dd")); 
    //System.out.println(transForDate1(transForMilliSecond("1970-01-01 00:00:00","yyyy-mm-dd HH:mm:ss"))); 
    //System.out.println(currentTimeStamp()); 
    //System.out.println(transForDate(currentTimeStamp())); 
    //System.out.println(new Date()); 
    //System.out.println(DateUtils.getDate()); 
    System.out.println(transFromTime("00:00:01")); 
    System.out.println(transToTime(transFromTime("15:01:13"))); 
  } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • idea創(chuàng)建springboot項目(版本只能選擇17和21)的解決方法

    idea創(chuàng)建springboot項目(版本只能選擇17和21)的解決方法

    idea2023創(chuàng)建spring boot項目時,java版本無法選擇11,本文主要介紹了idea創(chuàng)建springboot項目(版本只能選擇17和21),下面就來介紹一下解決方法,感興趣的可以了解一下
    2024-01-01
  • SpringBoot中實現(xiàn)定時任務(wù)的4種方式詳解

    SpringBoot中實現(xiàn)定時任務(wù)的4種方式詳解

    這篇文章主要介紹了SpringBoot中實現(xiàn)定時任務(wù)的4種方式詳解,在Springboot中定時任務(wù)是一項經(jīng)常能用到的功能,實現(xiàn)定時任務(wù)的方式有很多,今天來介紹常用的幾種,需要的朋友可以參考下
    2023-11-11
  • Spring boot測試找不到SpringRunner.class的問題

    Spring boot測試找不到SpringRunner.class的問題

    這篇文章主要介紹了Spring boot測試找不到SpringRunner.class的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 利用java實現(xiàn)二維碼和背景圖的合并

    利用java實現(xiàn)二維碼和背景圖的合并

    本文介紹如何使用java代碼將自動生成的二維碼放入背景模板中,對于java學(xué)習(xí)者或許有幫助,一起來看看。
    2016-07-07
  • Spring事件監(jiān)聽機(jī)制之@EventListener實現(xiàn)方式詳解

    Spring事件監(jiān)聽機(jī)制之@EventListener實現(xiàn)方式詳解

    這篇文章主要介紹了Spring事件監(jiān)聽機(jī)制之@EventListener實現(xiàn)方式詳解,ApplicationContext的refresh方法還是初始化了SimpleApplicationEventMulticaster,發(fā)送事件式還是先獲取ResolvableType類型,再獲取發(fā)送監(jiān)聽列表,需要的朋友可以參考下
    2023-12-12
  • java防反編譯最簡單的技巧分享

    java防反編譯最簡單的技巧分享

    這篇文章主要給大家分享了關(guān)于java防反編譯最簡單的技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • Spring Boot 配置文件(application.yml、application-dev.yml、application-test.yml)

    Spring Boot 配置文件(application.yml、application-dev.y

    本文主要介紹了Spring Boot 配置文件,主要包含application.yml、application-dev.yml、application-test.yml,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Java實現(xiàn)猜字小游戲

    Java實現(xiàn)猜字小游戲

    這篇文章給大家分享小編隨手寫的猜字小游戲,基于java代碼寫的,感興趣的朋友跟隨小編一起看看吧
    2019-11-11
  • JavaSE面試題之this與super關(guān)鍵字的區(qū)別詳解

    JavaSE面試題之this與super關(guān)鍵字的區(qū)別詳解

    this關(guān)鍵字用于引用當(dāng)前對象的引用,super關(guān)鍵字用于引用父類對象的引用,下面這篇文章主要給大家介紹了關(guān)于JavaSE面試題之this與super關(guān)鍵字區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • spring?retry實現(xiàn)方法請求重試的使用步驟

    spring?retry實現(xiàn)方法請求重試的使用步驟

    這篇文章主要介紹了spring?retry實現(xiàn)方法請求重試及使用步驟,本文分步驟通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07

最新評論

积石山| 平潭县| 集安市| 铁岭县| 望奎县| 伽师县| 麻城市| 汉沽区| 利川市| 白河县| 永泰县| 大英县| 邯郸市| 佳木斯市| 太保市| 阳西县| 雅安市| 凤翔县| 民和| 上思县| 莒南县| 富阳市| 河曲县| 特克斯县| 黄陵县| 东海县| 合阳县| 改则县| 乃东县| 连云港市| 长治县| 邵东县| 内丘县| 尤溪县| 宁津县| 壤塘县| 延边| 汝阳县| 城固县| 柞水县| 白朗县|