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

時間處理函數(shù)工具分享(時間戳計算)

 更新時間:2014年01月28日 15:17:27   作者:  
這篇文章主要介紹了時間處理函數(shù)工具,包括得到時間戳、周一、周末、時間更改、時間精確計算等功能

復(fù)制代碼 代碼如下:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 時間處理函數(shù)
 *
 * @20080509 15:50
 */
public class DateUtil {

 private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

 public static final String TIME_YEAR = "yyyy";

 public static final String TIME_MONEN = "MM";

 public static final String TIME_DAY = "dd";

 public static String getDate(String interval, Date starttime, String pattern) {
  Calendar temp = Calendar.getInstance(TimeZone.getDefault());
  temp.setTime(starttime);
  temp.add(temp.MONTH, Integer.parseInt(interval));
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  return sdf.format(temp.getTime());
 }

 /**
  * 將字符串類型轉(zhuǎn)換為時間類型
  *
  * @return
  */
 public static Date str2Date(String str) {
  Date d = null;
  SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
  try {
   d = sdf.parse(str);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return d;
 }

 public static Date str2Date(String str, String pattern) {
  Date d = null;
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  try {
   d = sdf.parse(str);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return d;
 }

 /**
  * 將時間格式化
  *
  * @return
  */
 public static Date DatePattern(Date date) {
  SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
  try {
   String dd = sdf.format(date);
   date = str2Date(dd);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return date;
 }

 /**
  * 將時間格式化
  */
 public static Date DatePattern(Date date, String pattern) {
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  try {
   String dd = sdf.format(date);
   date = str2Date(dd, pattern);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return date;
 }

 public static String date2Str(Date date) {
  SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
  return sdf.format(date);
 }

 public static String date2Str(Date date, String format) {
  SimpleDateFormat sdf = new SimpleDateFormat(format);
  return sdf.format(date);
 }

 /**
  * 獲取昨天
  *
  * @param date
  * @return
  * @throws Exception
  */
 public static Date getLastDate(Date date) {
  Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  calendar.setTime(date);

  calendar.add(calendar.DATE, -1);

  return str2Date(date2Str(calendar.getTime()));
 }
 /**
  * 獲取前幾天
  * @param date
  * @return
  */
 public static Date getBeforeDate(Date date,int dates) {
  Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  calendar.setTime(date);

  calendar.add(calendar.DATE, -dates);

  return str2Date(date2Str(calendar.getTime()));
 }

 /**
  * 獲取上周第一天(周一)
  *
  * @param date
  * @return
  * @throws Exception
  */
 public static Date getLastWeekStart(Date date) {
  Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  calendar.setTime(date);
  int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
  int startnum = 0;
  if (i == 0) {
   startnum = 7 + 6;
  } else {
   startnum = 7 + i - 1;
  }
  calendar.add(calendar.DATE, -startnum);

  return str2Date(date2Str(calendar.getTime()));
 }

 /**
  * 獲取上周最后一天(周末)
  *
  * @param date
  * @return
  * @throws Exception
  */
 public static Date getLastWeekEnd(Date date) {
  Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  calendar.setTime(date);
  int i = calendar.get(calendar.DAY_OF_WEEK) - 1;
  int endnum = 0;
  if (i == 0) {
   endnum = 7;
  } else {
   endnum = i;
  }
  calendar.add(calendar.DATE, -(endnum - 1));

  return str2Date(date2Str(calendar.getTime()));
 }

 /**
  * 根據(jù)年和月得到天數(shù)
  * @param num 月
  * @param year 年
  * @return
  */
 public static int getday(int num,int year){
  if(num==1 || num==3 || num==5 || num==7 || num==8 || num==10 || num==12){
   return 31;
  }else if(num==2){
   //判斷是否為閏年
   if(year%400==0 || (year%4==0 && year%100!=0)){
    return 29;
   }else{
    return 28;
   }

  }else{
   return 30;
  }
 }
 /*
  * 計算當(dāng)前日期距離下個月還有多少天
  */
 public static int getdaymis(Date time){
  int year = Integer.parseInt(
    new SimpleDateFormat(TIME_YEAR).format(time));//年

  int mm = Integer.parseInt(
    new SimpleDateFormat(TIME_MONEN).format(time));//月

  int dd = Integer.parseInt(
    new SimpleDateFormat(TIME_DAY).format(time));//日

  
  //獲取當(dāng)前年月的總天數(shù)
  int sdd = getday(mm,year);

  return sdd-dd;

  
 }
 /**
  * 日期轉(zhuǎn)秒數(shù)
  * @param dateString
  * @return
  */
 public static long getTime(String dateString) {
     long time = 0;
     try {
      Date ret = null;
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      ret = sdf.parse(dateString);
      time = ret.getTime()/1000;
     } catch (Exception e) {

     }
     return time;
    }

 
 /**
  * 精確計算時間差,精確到日
  * @param fistill 起始日期
  * @param nowtime 結(jié)束日期
  * @param type type為1返回年月日(如:2年3個月零5天) 否則返回總的天數(shù)
  * @return
  */
 public static String patienage(Date fistill,Date nowtime,Integer type){

  int fyear = Integer.parseInt(
    new SimpleDateFormat(TIME_YEAR).format(fistill));//起始年

  int fmm = Integer.parseInt(
    new SimpleDateFormat(TIME_MONEN).format(fistill));//起始月

  int fdd = Integer.parseInt(
    new SimpleDateFormat(TIME_DAY).format(fistill));//起始日

  
  int nyear = Integer.parseInt(
    new SimpleDateFormat(TIME_YEAR).format(nowtime));//結(jié)束年

  int nmm = Integer.parseInt(
    new SimpleDateFormat(TIME_MONEN).format(nowtime));//結(jié)束月

  int ndd = Integer.parseInt(
    new SimpleDateFormat(TIME_DAY).format(nowtime));//結(jié)束日

  int cyear = nyear - fyear;
  int cmm = nmm - fmm;
  int cdd = ndd - fdd;

  
  int zyear = 0;
  int zmm = 0;
  int zdd = 0;

  int countddd = 0;  //年月日累計天數(shù)

  if(cdd<0){
   if(cmm<0){
    zyear = cyear - 1;
    zmm = (cmm + 12)-1;
    int dd = getday(zmm,nyear-1);
    zdd = dd + cdd;

    
    countddd = zyear*365+zmm*30+zdd;

   }else if(cmm==0){
    zyear = cyear - 1;
    zmm = 12-1;
    int dd = getday(zmm,nyear-1);
    zdd = dd + cdd;

    countddd = zyear*365+zmm*30+zdd;

   }else{
    zyear = cyear;
    zmm = cmm - 1;
    int dd = getday(zmm,nyear);
    zdd = dd + cdd;

    countddd = zyear*365+zmm*30+zdd;

   }
  }else if(cdd==0){
   if(cmm<0){
    zyear = cyear - 1;
    zmm = cmm + 12;
    zdd = 0;

    countddd = zyear*365+zmm*30;

   }else if(cmm==0){
    zyear = cyear;
    zmm = 0;
    zdd = 0;

    countddd = zyear*365+zmm*30;

   }else{
    zyear = cyear;
    zmm = cmm;
    zdd = 0;

    countddd = zyear*365+zmm*30;
   }
  }else{
   if(cmm<0){
    zyear = cyear - 1;
    zmm = cmm + 12;
    zdd = cdd;

    countddd = zyear*365+zmm*30+zdd;
   }else if(cmm==0){
    zyear = cyear;
    zmm = 0;
    zdd = cdd;

    countddd = zyear*365+zmm*30+zdd;
   }else{
    zyear = cyear;
    zmm = cmm;
    zdd = cdd;

    countddd = zyear*365+zmm*30+zdd;
   }
  }
  String ptime = null;

  if(zdd!=0){
   if(zmm!=0){
    if(zyear!=0){
     ptime = zyear+"年"+zmm+"個月"+"零"+zdd+"天";
    }else{
     ptime = zmm+"個月"+"零"+zdd+"天";
    }
   }else{
    if(zyear!=0){
     ptime = zyear+"年"+"零"+zdd+"天";
    }else{
     ptime = zdd+"天";
    }
   }
  }else{
   if(zmm!=0){
    if(zyear!=0){
     ptime = zyear+"年"+zmm+"個月";
    }else{
     ptime = zmm+"個月";
    }
   }else{
    if(zyear!=0){
     ptime = zyear+"年";
    }else{
     ptime = null;
    }
   }
  }
  if(type==1){
   return ptime;   //返回年月日(如:2年3個月零5天)
  }else{
   return String.valueOf(countddd);  //返回總天數(shù)
  }

  
 }
 /**
  * 得到月數(shù)
  * @param year 年數(shù)差
  * @param month 月數(shù)差
  * @return
  */
 public static int getCmm(Integer year,Integer month){
  int zmm = 0;
  if(month < 0){
   zmm = (month + 12)+(year-1)*12;
  }else if(month == 0){
   zmm = year*12;
  }else{
   zmm = year*12+month;
  }
  return zmm;
 }

 

 /**
  * 改更現(xiàn)在時間
  */
 public static Date changeDate(String type, int value) {
  Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  if (type.equals("month")) {
   calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
  } else if (type.equals("date")) {
   calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
  }
  return calendar.getTime();
 }

 /**
  * 更改時間
  */
 public static Date changeDate(Date date, String type, int value) {
  if (date != null) {
   // Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
   Calendar calendar = new GregorianCalendar();
   calendar.setTime(date);
   // Calendar calendar = Calendar.
   if (type.equals("month")) {
    calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + value);
   } else if (type.equals("date")) {
    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + value);
   } else if (type.endsWith("year")) {
    calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + value);
   }
   return calendar.getTime();
  }
  return null;
 }

 /**
  * haoxw 比較時間是否在這兩個時間點(diǎn)之間
  *
  * @param time1
  * @param time2
  * @return
  */
 public static boolean checkTime(String time1, String time2) {
  Calendar calendar = Calendar.getInstance();
  Date date1 = calendar.getTime();
  Date date11 = DateUtil.str2Date(DateUtil.date2Str(date1, "yyyy-MM-dd") + " " + time1);// 起始時間

  Calendar c = Calendar.getInstance();
  c.add(Calendar.DATE, 1);
  Date date2 = c.getTime();
  Date date22 = DateUtil.str2Date(DateUtil.date2Str(date2, "yyyy-MM-dd") + " " + time2);// 終止時間

  Calendar scalendar = Calendar.getInstance();
  scalendar.setTime(date11);// 起始時間

  Calendar ecalendar = Calendar.getInstance();
  ecalendar.setTime(date22);// 終止時間

  Calendar calendarnow = Calendar.getInstance();

  if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
   return true;
  } else {
   return false;
  }

 }

 /**
  * haoxw 比較時間是否在這兩個時間點(diǎn)之間
  *
  * @param date11
  * @param date22
  * @return
  */
 public static boolean checkTime(Date date11, Date date22) {

  

  Calendar scalendar = Calendar.getInstance();
  scalendar.setTime(date11);// 起始時間

  Calendar ecalendar = Calendar.getInstance();
  ecalendar.setTime(date22);// 終止時間

  Calendar calendarnow = Calendar.getInstance();

  if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
   return true;
  } else {
   return false;
  }

 }


 public static boolean checkDate(String date1, String date2) {

  Date date11 = DateUtil.str2Date(date1, "yyyy-MM-dd HH:mm:ss");// 起始時間

  Date date22 = DateUtil.str2Date(date2, "yyyy-MM-dd HH:mm:ss");// 終止時間

  Calendar scalendar = Calendar.getInstance();
  scalendar.setTime(date11);// 起始時間

  Calendar ecalendar = Calendar.getInstance();
  ecalendar.setTime(date22);// 終止時間

  Calendar calendarnow = Calendar.getInstance();

  System.out.println(date11.toString());
  System.out.println(date22.toString());
  System.out.println(scalendar.toString());
  System.out.println(ecalendar.toString());
  System.out.println(calendarnow.toString());

  if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) {
   return true;
  } else {
   return false;
  }
 }

 /**
  * 獲取interval天之前的日期
  *
  * @param interval
  * @param starttime
  * @param pattern
  * @return
  */
 public static Date getIntervalDate(String interval, Date starttime, String pattern) {
  Calendar temp = Calendar.getInstance();
  temp.setTime(starttime);
  temp.add(temp.DATE, Integer.parseInt(interval));
  SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  String shijian = sdf.format(temp.getTime());
  return str2Date(shijian);
 }

 public static Date formatDate(Date date){
  SimpleDateFormat bartDateFormat =
  new SimpleDateFormat("yyyy-MM-dd");   
  System.out.println(bartDateFormat.format(date));
  SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");    
  try {
   Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));
  } catch (ParseException e) {    
   e.printStackTrace();
  }
  System.out.println(date.getTime());
  return date;

 }

 public static void main(String arf[]) {

  /*String time1 = "2009-05-07 19:20:00";
  String time2 = "2009-05-08 19:30:00";

  DateUtil d = new DateUtil();
  System.out.println(d.checkDate(time1, time2));
  System.out.println(d.date2Str(new Date()));*/

  //System.out.println(d.getIntervalDate("-3", new Date(), DEFAULT_PATTERN));
  Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
  System.out.println(calendar.toString());
  System.out.println(DateUtil.str2Date("20090731","yyyyMMdd"));

  System.out.println(DateUtil.getBeforeDate(new Date(),2 ));
  System.out.println(DateUtil.DatePattern(new Date()));

     SimpleDateFormat bartDateFormat =
  new SimpleDateFormat("yyyy-MM-dd");
  Date date = new Date();
  System.out.println("date;"+bartDateFormat.format(date));
  SimpleDateFormat bartDateFormat1 =new SimpleDateFormat("yyyy-MM-dd");    
  try {
   Date date1 = bartDateFormat1.parse(bartDateFormat.format(date));
   System.out.println("日期:"+date1);
  } catch (ParseException e) {    
   e.printStackTrace();
  }

 }
}

相關(guān)文章

  • SpringBoot集成Druid配置(yaml版本配置文件)詳解

    SpringBoot集成Druid配置(yaml版本配置文件)詳解

    這篇文章主要介紹了SpringBoot集成Druid配置(yaml版本配置文件),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • java字節(jié)碼框架ASM的深入學(xué)習(xí)

    java字節(jié)碼框架ASM的深入學(xué)習(xí)

    這篇文章主要給大家介紹了java中字節(jié)碼框架ASM的相關(guān)資料,文中介紹的非常詳細(xì),相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值,有需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • SpringBoot深入分析講解監(jiān)聽器模式下

    SpringBoot深入分析講解監(jiān)聽器模式下

    監(jiān)聽器模式,大家應(yīng)該并不陌生,主要的組成要素包括了事件、監(jiān)聽器以及廣播器;當(dāng)事件發(fā)生時,廣播器負(fù)責(zé)將事件傳遞給所有已知的監(jiān)聽器,而監(jiān)聽器會對自己感興趣的事件進(jìn)行處理
    2022-07-07
  • JDK源碼白話解讀之ThreadLocal篇

    JDK源碼白話解讀之ThreadLocal篇

    其實(shí)網(wǎng)上有很多關(guān)于ThreadLocal的文章了,有不少文章也已經(jīng)寫的非常好了。但是很多同學(xué)反應(yīng)還有一些部分沒有講解的十分清楚,還是有一定的疑惑沒有想的十分清楚
    2022-02-02
  • 快速上手Java單元測試框架JUnit5

    快速上手Java單元測試框架JUnit5

    今天給大家?guī)淼氖顷P(guān)于Java單元測試的相關(guān)知識,文章圍繞著Java單元測試框架JUnit5展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Spring中的Sentinel規(guī)則持久化解析

    Spring中的Sentinel規(guī)則持久化解析

    這篇文章主要介紹了Spring中的Sentinel規(guī)則持久化解析,具體內(nèi)容包括,Sentinel規(guī)則推送三種模式介紹,原始模式,拉模式,推模式,并對基于Nacos配置中心控制臺實(shí)現(xiàn)推送進(jìn)行詳盡介紹,需要的朋友可以參考下
    2023-09-09
  • 深入dom4j使用selectSingleNode方法報錯分析

    深入dom4j使用selectSingleNode方法報錯分析

    本篇文章是對dom4j使用selectSingleNode方法報錯進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Java?web實(shí)現(xiàn)簡單注冊功能

    Java?web實(shí)現(xiàn)簡單注冊功能

    這篇文章主要為大家詳細(xì)介紹了Java?web實(shí)現(xiàn)簡單注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java開發(fā)者結(jié)合Node.js編程入門教程

    Java開發(fā)者結(jié)合Node.js編程入門教程

    這篇文章主要介紹了Java開發(fā)者結(jié)合Node.js編程入門教程,我將先向您展示如何使用Java EE創(chuàng)建一個簡單的Rest服務(wù)來讀取 MongoDB數(shù)據(jù)庫。然后我會用node.js來實(shí)現(xiàn)相同的功能,需要的朋友可以參考下
    2014-09-09
  • springBoot中myBatisPlus的使用步驟及示例代碼

    springBoot中myBatisPlus的使用步驟及示例代碼

    MyBatis-Plus 是一個 MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開發(fā)效率,下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧
    2025-03-03

最新評論

阿拉善左旗| 海伦市| 孝感市| 南溪县| 绵阳市| 汉川市| 勃利县| 泾川县| 东丰县| 石泉县| 恩平市| 曲麻莱县| 方城县| 武川县| 磐安县| 修水县| 武穴市| 乌兰县| 同江市| 泽州县| 绥滨县| 牡丹江市| 舞阳县| 罗甸县| 怀集县| 得荣县| 黔西县| 岑溪市| 龙岩市| 徐汇区| 安新县| 商南县| 全州县| 平乡县| 县级市| 台湾省| 桐柏县| 军事| 甘孜县| 望谟县| 西和县|