欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)的日期處理類完整實(shí)例

 更新時間:2017年09月18日 11:48:14   作者:夏日娃  
這篇文章主要介紹了Java實(shí)現(xiàn)的日期處理類,結(jié)合完整實(shí)例形式分析了Java針對日期的獲取、運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)的日期處理類。分享給大家供大家參考,具體如下:

開發(fā)中常常要使用日期,先小結(jié)如下,以備后用。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class FormatTime {
  private final static SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
  private final static SimpleDateFormat sdfymdhm = new SimpleDateFormat("yyyyMMddHHmmss");
  private final static SimpleDateFormat sdfymdhms =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  /**
   *
  * @Title: getCurrentDay
  * @Description: TODO 獲取當(dāng)天時間(20161109)
  * @return
   */
  public static String getCurrentDay(){
    return sdf.format(new Date());
  }
  /**
   *
  * @Title: fTime2
  * @Description: TODO 獲取time這個日期以前dayAgo天的日期
  * @return
   */
  public static String fTime(String time,int dayAgo){
    Date date = null;
    try {
      date = sdf.parse(time);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if(dayAgo>0){
      calendar.add(Calendar.DAY_OF_MONTH, -dayAgo);//前15天數(shù)據(jù)
      date = calendar.getTime();
      calendar.setTime(date);
    }
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String mon="";
    String d="";
    if(month<10){
      mon="0"+month;
    }else{
      mon=month+"";
    }
    if(day<10){
      d="0"+day;
    }else{
      d=""+day;
    }
    String ret=year+""+mon+""+d;
    return ret;
  }
  /**
   *
  * @Title: fTime2
  * @Description: TODO 獲取time這個日期以后dayAfter天的日期
  * @return
   */
  public static String fTime2(String time,int dayAfter){
    Date date = null;
    try {
      date = sdf.parse(time);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if(dayAfter>0){
      calendar.add(Calendar.DAY_OF_MONTH, +dayAfter);//后15天數(shù)據(jù)
      date = calendar.getTime();
      calendar.setTime(date);
    }
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String mon="";
    String d="";
    if(month<10){
      mon="0"+month;
    }else{
      mon=month+"";
    }
    if(day<10){
      d="0"+day;
    }else{
      d=""+day;
    }
    String ret=year+""+mon+""+d;
    return ret;
  }
  /**
   *
  * @Title: getDefaultTime
  * @Description: TODO 獲取昨天的日期
  * @return
   */
  public static String getDefaultTime(){
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -1);//前1天
    Date date = calendar.getTime();
    String time=sdf.format(date);
    return time;
  }
  /**
   *
  * @Title: getSunday
  * @Description: TODO 獲取最近一個星期天
  * @return
   */
  public static String getSunday(){
    SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
    Calendar c = Calendar.getInstance();
    c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    return f.format(c.getTime());
  }
  /**
   *
  * @Title: getMonthFirstDay
  * @Description: TODO 獲取本月第一天
  * @return
   */
  public static String getCurrentMonthFirstDay(){
    Calendar  cal_1=Calendar.getInstance();//獲取當(dāng)前日期
    cal_1.add(Calendar.MONTH, 0);
    cal_1.set(Calendar.DAY_OF_MONTH,1);//設(shè)置為1號,當(dāng)前日期既為本月第一天
    String firstDay = sdf.format(cal_1.getTime());
    return firstDay;
  }
  /**
   *
  * @Title: getMonthFirstDay
  * @Description: TODO 獲取上月第一天
  * @return
   */
  public static String getPreviousMonthFirstDay(){
   //獲取當(dāng)前月第一天:
  Calendar c = Calendar.getInstance();
  c.add(Calendar.MONTH, -1);
  c.set(Calendar.DAY_OF_MONTH,1);//設(shè)置為1號,當(dāng)前日期既為本月第一天
  String first = sdf.format(c.getTime());
  return first;
  }
  /**
   *
  * @Title: getMonthFirstDay
  * @Description: TODO 獲取上月最后一天
  * @return
   */
  public static String getPreviousMonthLastDay(){
  //獲取當(dāng)前月最后一天
  Calendar ca = Calendar.getInstance();
  ca.set(Calendar.DAY_OF_MONTH,0);//
  String lastDay = sdf.format(ca.getTime());
  return lastDay;
  }
  /**
   *
  * @Title: getCurrentMonthLastDay
  * @Description: TODO 獲取指定時間最后一天
  * @return
   */
  public static String getCurrentMonthLastDay(String time){
    Date date =null;
   try {
    date= sdf.parse(time);
  } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  //獲取當(dāng)前月最后一天
  Calendar ca = Calendar.getInstance();
  ca.setTime(date);
  ca.set(Calendar.DAY_OF_MONTH,
      ca.getActualMaximum(Calendar.DAY_OF_MONTH)); //
  String lastDay = sdf.format(ca.getTime());
  return lastDay;
  }
  /***
   *
  * @Title: getCurrentWeekDay
  * @Description: TODO 獲取本周周一
   */
  public static String getCurrentMonday(){
     Calendar cal = Calendar.getInstance();
     cal.setFirstDayOfWeek(Calendar.MONDAY);//將每周第一天設(shè)為星期一,默認(rèn)是星期天
     cal.add(Calendar.DATE, 0);
     cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
     String monday = sdf.format(cal.getTime());
     return monday;
  }
  /***
   *
  * @Title: getPreviousSunday
  * @Description: TODO 獲取上周周日
   */
  public static String getPreviousSunday(){
     Calendar cal = Calendar.getInstance();
     cal.setFirstDayOfWeek(Calendar.MONDAY);//將每周第一天設(shè)為星期一,默認(rèn)是星期天
     cal.add(Calendar.DATE, -1*7);
     cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
     String sunday =sdf.format(cal.getTime());
     return sunday;
  }
  /**
   *
  * @Title: getMiniSencond
  * @Description: TODO 將日期轉(zhuǎn)換為毫秒數(shù)
  * @param str
  * @return
   */
  public static String getMiniSencond(String str){
    long millionSeconds=0;
    try {
      millionSeconds = sdfymdhm.parse(str).getTime();//毫秒
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return millionSeconds+"";
  }
   /**
   *
  * @Title: getDateSencond
  * @Description: TODO 將日期轉(zhuǎn)換為毫秒數(shù)
  * @param str
  * @return
   */
  public static long getDateSencond(String str){
    long millionSeconds=0;
    try {
      millionSeconds = sdfymdhms.parse(str).getTime();//毫秒
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return millionSeconds;
  }
  /**
   * 計算日期相差天數(shù)
   * @param str1
   * @param str2
   * @return
   */
  public static int getDistanceOfTwoDate(String str1,String str2){
    int result=0;
    try{
      Date date1 = sdf.parse(str1);
      Date date2 =sdf.parse(str2);
      Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date1);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(date2);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        result = day1-day2;
    }catch(Exception e){
      e.printStackTrace();
    }
    return result;
  }
  /**
   *
  * @Title: long2Date
  * @Description: TODO long 轉(zhuǎn)日期(年-月-日 時-分-秒)
  * @param timestamp
  * @return
   */
  public static String longToDate(Long msecond){
    Date date = new Date(msecond);
    return sdfymdhms.format(date);
  }
}

PS:這里再為大家推薦幾款關(guān)于日期與天數(shù)計算的在線工具供大家使用:

在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在線萬年歷日歷:
http://tools.jb51.net/bianmin/wannianli

在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《java日期與時間操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • Java數(shù)據(jù)類型的規(guī)則

    Java數(shù)據(jù)類型的規(guī)則

    這篇文章主要介紹了Java數(shù)據(jù)類型的規(guī)則的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • spring-retry組件的使用教程

    spring-retry組件的使用教程

    Spring Retry的主要目的是為了提高系統(tǒng)的可靠性和容錯性,當(dāng)方法調(diào)用失敗時,Spring Retry可以在不影響系統(tǒng)性能的情況下,自動進(jìn)行重試,從而減少故障對系統(tǒng)的影響,這篇文章主要介紹了spring-retry組件的使用,需要的朋友可以參考下
    2023-06-06
  • springboot集成springCloud中g(shù)ateway時啟動報錯的解決

    springboot集成springCloud中g(shù)ateway時啟動報錯的解決

    這篇文章主要介紹了springboot集成springCloud中g(shù)ateway時啟動報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 如何使用IDEA 搭建 SpringCloud 項(xiàng)目

    如何使用IDEA 搭建 SpringCloud 項(xiàng)目

    所謂微服務(wù),就是要把整個業(yè)務(wù)模塊拆分成多個各司其職的小模塊,做到單一職責(zé)原則,不會重復(fù)開發(fā)相同的業(yè)務(wù)代碼,實(shí)現(xiàn)真正意義上的高內(nèi)聚、低耦合,這篇文章主要介紹了如何使用IDEA 搭建 SpringCloud 項(xiàng)目,需要的朋友可以參考下
    2023-11-11
  • java NIO 詳解

    java NIO 詳解

    Java NIO(New IO)是從Java 1.4版本開始引入的一個新的IO API,可以替代標(biāo)準(zhǔn)的Java IO API。本系列教程將有助于你學(xué)習(xí)和理解Java NIO。
    2014-10-10
  • 詳解關(guān)于java文件下載文件名亂碼問題解決方案

    詳解關(guān)于java文件下載文件名亂碼問題解決方案

    這篇文章主要介紹了詳解關(guān)于java文件下載文件名亂碼問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 使用Springboot實(shí)現(xiàn)word在線編輯保存

    使用Springboot實(shí)現(xiàn)word在線編輯保存

    PageOffice目前支持的Web編程語言及架構(gòu)有:Java(JSP、SSH、MVC等),ASP.NET(C#、VB.NET、MVC、Razor等),PHP,ASP,本篇文章就帶你使用Springboot整合PageOffice實(shí)現(xiàn)word在線編輯保存
    2021-08-08
  • java字符串常用操作方法(查找、截取、分割)

    java字符串常用操作方法(查找、截取、分割)

    今天小編就為大家分享一篇java字符串常用操作方法(查找、截取、分割),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • java類的定義與使用舉例詳解

    java類的定義與使用舉例詳解

    這篇文章主要給大家介紹了關(guān)于java類的定義與使用的相關(guān)資料,類的方法是用來定義類的行為,在方法中通過操作類的成員變量、編寫業(yè)務(wù)邏輯、返回 結(jié)果等實(shí)現(xiàn)類的業(yè)務(wù)行為,需要的朋友可以參考下
    2023-11-11
  • Servlet開發(fā)JavaWeb工程示例詳解

    Servlet開發(fā)JavaWeb工程示例詳解

    這篇文章主要介紹了Servlet開發(fā)JavaWeb工程示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論