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

java日期處理工具類(lèi)

 更新時(shí)間:2020年06月28日 09:39:06   作者:jiangbang  
這篇文章主要為大家詳細(xì)介紹了java日期處理工具類(lèi),其次還介紹了日期處理的基礎(chǔ)知識(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文針對(duì)日期處理進(jìn)行學(xué)習(xí)使用,主要分為兩部分,下面為大家具體介紹一下

第一部分:日期處理基礎(chǔ)知識(shí)

Date 類(lèi)

作用:最主要的作用就是獲得當(dāng)前時(shí)間
將日期轉(zhuǎn)換為標(biāo)準(zhǔn)格式

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = sdf.format(date);
System.out.println(“2015-10-16 14:59:52”);

將String轉(zhuǎn)換為Date類(lèi)型

String day = "2014-6-5 10:30:30";
SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2 = d2.parse(day);
System.out.println(“Thu Jun 05 10:30:30 CST 2014”);

Calendar 類(lèi)的應(yīng)用

java.util.Calendar 類(lèi)是一個(gè)抽象類(lèi),可以通過(guò)調(diào)用 getInstance() 靜態(tài)方法獲取一個(gè) Calendar 對(duì)象,此對(duì)象已由當(dāng)前日期時(shí)間初始化,即默認(rèn)代表當(dāng)前時(shí)間

 Calendar c = Calendar.getInstance();
int year = c.get(Calender.YEAR);
int month= c.get(Calender.MONTH)+1; //獲取月份,0表示1月份
int day = c.get(Calender.DAY_OF_MONTH);
int hour= c.get(Calender.HOUR_OF_DAY);
int minute= c.get(Calender.MINUTE);
int second = c.get(Calender.SECOND);

比較2個(gè)時(shí)間相差的月份

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateTime d1 = new DateTime(format.parse("2016-10-31 00:00:00"));
DateTime d2 = new DateTime(format.parse("2015-1-31 00:00:00"));
System.out.println(Math.abs(Months.monthsBetween(d1,d2).getMonths()));

第二部分:日期處理工具類(lèi)

package com.analysys.website.control;
 
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
 
/** 
 * 日期處理工具類(lèi) 
 * @author dylan_xu 
 * @date Mar 11, 2012 
 * @modified by 
 * @modified date 
 * @since JDK1.6 
 * @see com.util.DateUtil 
 */ 
 
public class DateUtil { 
 // ~ Static fields/initializers 
 // ============================================= 
 
 private static Logger logger = Logger.getLogger(DateUtil.class); 
 private static String defaultDatePattern = null; 
 private static String timePattern = "HH:mm"; 
 private static Calendar cale = Calendar.getInstance(); 
 public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S"; 
 /** 日期格式y(tǒng)yyy-MM字符串常量 */ 
 private static final String MONTH_FORMAT = "yyyy-MM"; 
 /** 日期格式y(tǒng)yyy-MM-dd字符串常量 */ 
 private static final String DATE_FORMAT = "yyyy-MM-dd"; 
 /** 日期格式HH:mm:ss字符串常量 */ 
 private static final String HOUR_FORMAT = "HH:mm:ss"; 
 /** 日期格式y(tǒng)yyy-MM-dd HH:mm:ss字符串常量 */ 
 private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; 
 /** 某天開(kāi)始時(shí)分秒字符串常量 00:00:00 */ 
 private static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00"; 
 /** 某天結(jié)束時(shí)分秒字符串常量 23:59:59 */ 
 public static final String DAY_END_STRING_HHMMSS = " 23:59:59"; 
 private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT); 
 private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT); 
 private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(DATETIME_FORMAT); 
  
 
 // ~ Methods 
 // ================================================================ 
 
 public DateUtil() { 
 } 
 
 /** 
  * 獲得服務(wù)器當(dāng)前日期及時(shí)間,以格式為:yyyy-MM-dd HH:mm:ss的日期字符串形式返回 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getDateTime() { 
  try { 
   return sdf_datetime_format.format(cale.getTime()); 
  } catch (Exception e) { 
   logger.debug("DateUtil.getDateTime():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 獲得服務(wù)器當(dāng)前日期,以格式為:yyyy-MM-dd的日期字符串形式返回 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getDate() { 
  try { 
   return sdf_date_format.format(cale.getTime()); 
  } catch (Exception e) { 
   logger.debug("DateUtil.getDate():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 獲得服務(wù)器當(dāng)前時(shí)間,以格式為:HH:mm:ss的日期字符串形式返回 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getTime() { 
  String temp = " "; 
  try { 
   temp += sdf_hour_format.format(cale.getTime()); 
   return temp; 
  } catch (Exception e) { 
   logger.debug("DateUtil.getTime():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 統(tǒng)計(jì)時(shí)開(kāi)始日期的默認(rèn)值 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getStartDate() { 
  try { 
   return getYear() + "-01-01"; 
  } catch (Exception e) { 
   logger.debug("DateUtil.getStartDate():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 統(tǒng)計(jì)時(shí)結(jié)束日期的默認(rèn)值 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getEndDate() { 
  try { 
   return getDate(); 
  } catch (Exception e) { 
   logger.debug("DateUtil.getEndDate():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 獲得服務(wù)器當(dāng)前日期的年份 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getYear() { 
  try { 
   return String.valueOf(cale.get(Calendar.YEAR)); 
  } catch (Exception e) { 
   logger.debug("DateUtil.getYear():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 獲得服務(wù)器當(dāng)前日期的月份 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getMonth() { 
  try { 
   java.text.DecimalFormat df = new java.text.DecimalFormat(); 
   df.applyPattern("00;00"); 
   return df.format((cale.get(Calendar.MONTH) + 1)); 
  } catch (Exception e) { 
   logger.debug("DateUtil.getMonth():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 獲得服務(wù)器在當(dāng)前月中天數(shù) 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getDay() { 
  try { 
   return String.valueOf(cale.get(Calendar.DAY_OF_MONTH)); 
  } catch (Exception e) { 
   logger.debug("DateUtil.getDay():" + e.getMessage()); 
   return ""; 
  } 
 } 
 
 /** 
  * 比較兩個(gè)日期相差的天數(shù) 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param date1 
  * @param date2 
  * @return 
  */ 
 public static int getMargin(String date1, String date2) { 
  int margin; 
  try { 
   ParsePosition pos = new ParsePosition(0); 
   ParsePosition pos1 = new ParsePosition(0); 
   Date dt1 = sdf_date_format.parse(date1, pos); 
   Date dt2 = sdf_date_format.parse(date2, pos1); 
   long l = dt1.getTime() - dt2.getTime(); 
   margin = (int) (l / (24 * 60 * 60 * 1000)); 
   return margin; 
  } catch (Exception e) { 
   logger.debug("DateUtil.getMargin():" + e.toString()); 
   return 0; 
  } 
 } 
 
 /** 
  * 比較兩個(gè)日期相差的天數(shù) 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param date1 
  * @param date2 
  * @return 
  */ 
 public static double getDoubleMargin(String date1, String date2) { 
  double margin; 
  try { 
   ParsePosition pos = new ParsePosition(0); 
   ParsePosition pos1 = new ParsePosition(0); 
   Date dt1 = sdf_datetime_format.parse(date1, pos); 
   Date dt2 = sdf_datetime_format.parse(date2, pos1); 
   long l = dt1.getTime() - dt2.getTime(); 
   margin = (l / (24 * 60 * 60 * 1000.00)); 
   return margin; 
  } catch (Exception e) { 
   logger.debug("DateUtil.getMargin():" + e.toString()); 
   return 0; 
  } 
 } 
 
 /** 
  * 比較兩個(gè)日期相差的月數(shù) 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param date1 
  * @param date2 
  * @return 
  */ 
 public static int getMonthMargin(String date1, String date2) { 
  int margin; 
  try { 
   margin = (Integer.parseInt(date2.substring(0, 4)) - Integer.parseInt(date1.substring(0, 4))) * 12; 
   margin += (Integer.parseInt(date2.substring(4, 7).replaceAll("-0", 
     "-")) - Integer.parseInt(date1.substring(4, 7).replaceAll("-0", "-"))); 
   return margin; 
  } catch (Exception e) { 
   logger.debug("DateUtil.getMargin():" + e.toString()); 
   return 0; 
  } 
 } 
 
 /** 
  * 返回日期加X(jué)天后的日期 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param date 
  * @param i 
  * @return 
  */ 
 public static String addDay(String date, int i) { 
  try { 
   GregorianCalendar gCal = new GregorianCalendar( 
     Integer.parseInt(date.substring(0, 4)), 
     Integer.parseInt(date.substring(5, 7)) - 1, 
     Integer.parseInt(date.substring(8, 10))); 
   gCal.add(GregorianCalendar.DATE, i); 
   return sdf_date_format.format(gCal.getTime()); 
  } catch (Exception e) { 
   logger.debug("DateUtil.addDay():" + e.toString()); 
   return getDate(); 
  } 
 } 
 
 /** 
  * 返回日期加X(jué)月后的日期 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param date 
  * @param i 
  * @return 
  */ 
 public static String addMonth(String date, int i) { 
  try { 
   GregorianCalendar gCal = new GregorianCalendar( 
     Integer.parseInt(date.substring(0, 4)), 
     Integer.parseInt(date.substring(5, 7)) - 1, 
     Integer.parseInt(date.substring(8, 10))); 
   gCal.add(GregorianCalendar.MONTH, i); 
   return sdf_date_format.format(gCal.getTime()); 
  } catch (Exception e) { 
   logger.debug("DateUtil.addMonth():" + e.toString()); 
   return getDate(); 
  } 
 } 
 
 /** 
  * 返回日期加X(jué)年后的日期 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param date 
  * @param i 
  * @return 
  */ 
 public static String addYear(String date, int i) { 
  try { 
   GregorianCalendar gCal = new GregorianCalendar( 
     Integer.parseInt(date.substring(0, 4)), 
     Integer.parseInt(date.substring(5, 7)) - 1, 
     Integer.parseInt(date.substring(8, 10))); 
   gCal.add(GregorianCalendar.YEAR, i); 
   return sdf_date_format.format(gCal.getTime()); 
  } catch (Exception e) { 
   logger.debug("DateUtil.addYear():" + e.toString()); 
   return ""; 
  } 
 } 
 
 /** 
  * 返回某年某月中的最大天 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param year 
  * @param month 
  * @return 
  */ 
 public static int getMaxDay(int iyear, int imonth) { 
  int day = 0; 
  try { 
   if (imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7 
     || imonth == 8 || imonth == 10 || imonth == 12) { 
    day = 31; 
   } else if (imonth == 4 || imonth == 6 || imonth == 9 || imonth == 11) { 
    day = 30; 
   } else if ((0 == (iyear % 4)) && (0 != (iyear % 100)) || (0 == (iyear % 400))) { 
    day = 29; 
   } else { 
    day = 28; 
   } 
   return day; 
  } catch (Exception e) { 
   logger.debug("DateUtil.getMonthDay():" + e.toString()); 
   return 1; 
  } 
 } 
 
 /** 
  * 格式化日期 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param orgDate 
  * @param Type 
  * @param Span 
  * @return 
  */ 
 @SuppressWarnings("static-access") 
 public String rollDate(String orgDate, int Type, int Span) { 
  try { 
   String temp = ""; 
   int iyear, imonth, iday; 
   int iPos = 0; 
   char seperater = '-'; 
   if (orgDate == null || orgDate.length() < 6) { 
    return ""; 
   } 
 
   iPos = orgDate.indexOf(seperater); 
   if (iPos > 0) { 
    iyear = Integer.parseInt(orgDate.substring(0, iPos)); 
    temp = orgDate.substring(iPos + 1); 
   } else { 
    iyear = Integer.parseInt(orgDate.substring(0, 4)); 
    temp = orgDate.substring(4); 
   } 
 
   iPos = temp.indexOf(seperater); 
   if (iPos > 0) { 
    imonth = Integer.parseInt(temp.substring(0, iPos)); 
    temp = temp.substring(iPos + 1); 
   } else { 
    imonth = Integer.parseInt(temp.substring(0, 2)); 
    temp = temp.substring(2); 
   } 
 
   imonth--; 
   if (imonth < 0 || imonth > 11) { 
    imonth = 0; 
   } 
 
   iday = Integer.parseInt(temp); 
   if (iday < 1 || iday > 31) 
    iday = 1; 
 
   Calendar orgcale = Calendar.getInstance(); 
   orgcale.set(iyear, imonth, iday); 
   temp = this.rollDate(orgcale, Type, Span); 
   return temp; 
  } catch (Exception e) { 
   return ""; 
  } 
 } 
 
 public static String rollDate(Calendar cal, int Type, int Span) { 
  try { 
   String temp = ""; 
   Calendar rolcale; 
   rolcale = cal; 
   rolcale.add(Type, Span); 
   temp = sdf_date_format.format(rolcale.getTime()); 
   return temp; 
  } catch (Exception e) { 
   return ""; 
  } 
 } 
 
 /** 
  * 返回默認(rèn)的日期格式 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static synchronized String getDatePattern() { 
  defaultDatePattern = "yyyy-MM-dd"; 
  return defaultDatePattern; 
 } 
 
 /** 
  * 將指定日期按默認(rèn)格式進(jìn)行格式代化成字符串后輸出如:yyyy-MM-dd 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param aDate 
  * @return 
  */ 
 public static final String getDate(Date aDate) { 
  SimpleDateFormat df = null; 
  String returnValue = ""; 
  if (aDate != null) { 
   df = new SimpleDateFormat(getDatePattern()); 
   returnValue = df.format(aDate); 
  } 
  return (returnValue); 
 } 
 
 /** 
  * 取得給定日期的時(shí)間字符串,格式為當(dāng)前默認(rèn)時(shí)間格式 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param theTime 
  * @return 
  */ 
 public static String getTimeNow(Date theTime) { 
  return getDateTime(timePattern, theTime); 
 } 
 
 /** 
  * 取得當(dāng)前時(shí)間的Calendar日歷對(duì)象 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  * @throws ParseException 
  */ 
 public Calendar getToday() throws ParseException { 
  Date today = new Date(); 
  SimpleDateFormat df = new SimpleDateFormat(getDatePattern()); 
  String todayAsString = df.format(today); 
  Calendar cal = new GregorianCalendar(); 
  cal.setTime(convertStringToDate(todayAsString)); 
  return cal; 
 } 
 
 /** 
  * 將日期類(lèi)轉(zhuǎn)換成指定格式的字符串形式 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param aMask 
  * @param aDate 
  * @return 
  */ 
 public static final String getDateTime(String aMask, Date aDate) { 
  SimpleDateFormat df = null; 
  String returnValue = ""; 
 
  if (aDate == null) { 
   logger.error("aDate is null!"); 
  } else { 
   df = new SimpleDateFormat(aMask); 
   returnValue = df.format(aDate); 
  } 
  return (returnValue); 
 } 
 
 /** 
  * 將指定的日期轉(zhuǎn)換成默認(rèn)格式的字符串形式 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param aDate 
  * @return 
  */ 
 public static final String convertDateToString(Date aDate) { 
  return getDateTime(getDatePattern(), aDate); 
 } 
 
 /** 
  * 將日期字符串按指定格式轉(zhuǎn)換成日期類(lèi)型 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param aMask 指定的日期格式,如:yyyy-MM-dd 
  * @param strDate 待轉(zhuǎn)換的日期字符串 
  * @return 
  * @throws ParseException 
  */ 
 public static final Date convertStringToDate(String aMask, String strDate) 
   throws ParseException { 
  SimpleDateFormat df = null; 
  Date date = null; 
  df = new SimpleDateFormat(aMask); 
 
  if (logger.isDebugEnabled()) { 
   logger.debug("converting '" + strDate + "' to date with mask '" + aMask + "'"); 
  } 
  try { 
   date = df.parse(strDate); 
  } catch (ParseException pe) { 
   logger.error("ParseException: " + pe); 
   throw pe; 
  } 
  return (date); 
 } 
 
 /** 
  * 將日期字符串按默認(rèn)格式轉(zhuǎn)換成日期類(lèi)型 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param strDate 
  * @return 
  * @throws ParseException 
  */ 
 public static Date convertStringToDate(String strDate) 
   throws ParseException { 
  Date aDate = null; 
 
  try { 
   if (logger.isDebugEnabled()) { 
    logger.debug("converting date with pattern: " + getDatePattern()); 
   } 
   aDate = convertStringToDate(getDatePattern(), strDate); 
  } catch (ParseException pe) { 
   logger.error("Could not convert '" + strDate + "' to a date, throwing exception"); 
   throw new ParseException(pe.getMessage(), pe.getErrorOffset()); 
  } 
  return aDate; 
 } 
 
 /** 
  * 返回一個(gè)JAVA簡(jiǎn)單類(lèi)型的日期字符串 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 public static String getSimpleDateFormat() { 
  SimpleDateFormat formatter = new SimpleDateFormat(); 
  String NDateTime = formatter.format(new Date()); 
  return NDateTime; 
 } 
  
 /** 
  * 將指定字符串格式的日期與當(dāng)前時(shí)間比較 
  * @author DYLAN 
  * @date Feb 17, 2012 
  * @param strDate 需要比較時(shí)間 
  * @return 
  *  <p> 
  *  int code 
  *  <ul> 
  *  <li>-1 當(dāng)前時(shí)間 < 比較時(shí)間 </li> 
  *  <li> 0 當(dāng)前時(shí)間 = 比較時(shí)間 </li> 
  *  <li>>=1當(dāng)前時(shí)間 > 比較時(shí)間 </li> 
  *  </ul> 
  *  </p> 
  */ 
 public static int compareToCurTime (String strDate) { 
  if (StringUtils.isBlank(strDate)) { 
   return -1; 
  } 
  Date curTime = cale.getTime(); 
  String strCurTime = null; 
  try { 
   strCurTime = sdf_datetime_format.format(curTime); 
  } catch (Exception e) { 
   if (logger.isDebugEnabled()) { 
    logger.debug("[Could not format '" + strDate + "' to a date, throwing exception:" + e.getLocalizedMessage() + "]"); 
   } 
  } 
  if (StringUtils.isNotBlank(strCurTime)) { 
   return strCurTime.compareTo(strDate); 
  } 
  return -1; 
 } 
  
 /** 
  * 為查詢?nèi)掌谔砑幼钚r(shí)間 
  * 
  * @param 目標(biāo)類(lèi)型Date 
  * @param 轉(zhuǎn)換參數(shù)Date 
  * @return 
  */ 
 @SuppressWarnings("deprecation") 
 public static Date addStartTime(Date param) { 
  Date date = param; 
  try { 
   date.setHours(0); 
   date.setMinutes(0); 
   date.setSeconds(0); 
   return date; 
  } catch (Exception ex) { 
   return date; 
  } 
 } 
 
 /** 
  * 為查詢?nèi)掌谔砑幼畲髸r(shí)間 
  * 
  * @param 目標(biāo)類(lèi)型Date 
  * @param 轉(zhuǎn)換參數(shù)Date 
  * @return 
  */ 
 @SuppressWarnings("deprecation") 
 public static Date addEndTime(Date param) { 
  Date date = param; 
  try { 
   date.setHours(23); 
   date.setMinutes(59); 
   date.setSeconds(0); 
   return date; 
  } catch (Exception ex) { 
   return date; 
  } 
 } 
 
 /** 
  * 返回系統(tǒng)現(xiàn)在年份中指定月份的天數(shù) 
  * 
  * @param 月份month 
  * @return 指定月的總天數(shù) 
  */ 
 @SuppressWarnings("deprecation") 
 public static String getMonthLastDay(int month) { 
  Date date = new Date(); 
  int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 
    { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; 
  int year = date.getYear() + 1900; 
  if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 
   return day[1][month] + ""; 
  } else { 
   return day[0][month] + ""; 
  } 
 } 
 
 /** 
  * 返回指定年份中指定月份的天數(shù) 
  * 
  * @param 年份year 
  * @param 月份month 
  * @return 指定月的總天數(shù) 
  */ 
 public static String getMonthLastDay(int year, int month) { 
  int[][] day = { { 0, 30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, 
    { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; 
  if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 
   return day[1][month] + ""; 
  } else { 
   return day[0][month] + ""; 
  } 
 } 
 
 /** 
  * 判斷是平年還是閏年 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @param year 
  * @return 
  */ 
 public static boolean isLeapyear(int year) { 
  if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) { 
   return true; 
  } else { 
   return false; 
  } 
 } 
 
 /** 
  * 取得當(dāng)前時(shí)間的日戳 
  * @author dylan_xu 
  * @date Mar 11, 2012 
  * @return 
  */ 
 @SuppressWarnings("deprecation") 
 public static String getTimestamp() { 
  Date date = cale.getTime(); 
  String timestamp = "" + (date.getYear() + 1900) + date.getMonth() 
    + date.getDate() + date.getMinutes() + date.getSeconds() 
    + date.getTime(); 
  return timestamp; 
 } 
 
 /** 
  * 取得指定時(shí)間的日戳 
  * 
  * @return 
  */ 
 @SuppressWarnings("deprecation") 
 public static String getTimestamp(Date date) { 
  String timestamp = "" + (date.getYear() + 1900) + date.getMonth() 
    + date.getDate() + date.getMinutes() + date.getSeconds() 
    + date.getTime(); 
  return timestamp; 
 } 
 
 public static void main(String[] args) { 
  System.out.println(getYear() + "|" + getMonth() + "|" + getDate()); 
 } 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • maven引入本地jar包運(yùn)行報(bào)錯(cuò)java.lang.NoClassDefFoundError解決

    maven引入本地jar包運(yùn)行報(bào)錯(cuò)java.lang.NoClassDefFoundError解決

    這篇文章主要為大家介紹了maven引入本地jar包運(yùn)行報(bào)錯(cuò)java.lang.NoClassDefFoundError解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實(shí)現(xiàn)

    Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實(shí)現(xiàn)

    這篇文章主要介紹了Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • MyBatis批量插入的五種方式

    MyBatis批量插入的五種方式

    這篇文章主要介紹了MyBatis批量插入的五種方式,每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • Java測(cè)試框架Mockito的簡(jiǎn)明教程

    Java測(cè)試框架Mockito的簡(jiǎn)明教程

    這篇文章主要介紹了Java測(cè)試框架Mockito的簡(jiǎn)明教程,Mock 測(cè)試是單元測(cè)試的重要方法之一。本文介紹了基于 Java 語(yǔ)言的 Mock 測(cè)試框架 – Mockito 的使用。,需要的朋友可以參考下
    2019-06-06
  • 解讀@Scheduled任務(wù)調(diào)度/定時(shí)任務(wù)非分布式

    解讀@Scheduled任務(wù)調(diào)度/定時(shí)任務(wù)非分布式

    這篇文章主要介紹了解讀@Scheduled任務(wù)調(diào)度/定時(shí)任務(wù)非分布式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 微信小程序完整項(xiàng)目實(shí)戰(zhàn)記錄(前端+SpringBoot后端)

    微信小程序完整項(xiàng)目實(shí)戰(zhàn)記錄(前端+SpringBoot后端)

    隨著微信小程序的流行,越來(lái)越多的開(kāi)發(fā)者開(kāi)始涉足小程序開(kāi)發(fā),下面這篇文章主要給大家介紹了關(guān)于微信小程序完整項(xiàng)目實(shí)戰(zhàn)的相關(guān)資料,項(xiàng)目包括前端+SpringBoot后端,需要的朋友可以參考下
    2024-09-09
  • 一文詳解Java?Condition的await和signal等待通知機(jī)制

    一文詳解Java?Condition的await和signal等待通知機(jī)制

    這篇文章主要為大家詳細(xì)介紹了Java?Condition的await和signal等待通知機(jī)制的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2025-02-02
  • IDEA常用插件之代碼掃描SonarLint詳解

    IDEA常用插件之代碼掃描SonarLint詳解

    SonarLint是一款用于代碼掃描的插件,可以幫助查找隱藏的bug,下載并安裝插件后,右鍵點(diǎn)擊項(xiàng)目并選擇“Analyze”、“AnalyzewithSonarLint”,掃描完成后可以在下方查看報(bào)告
    2025-01-01
  • Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn)

    Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn)

    這篇文章主要介紹了Java使用Maven BOM統(tǒng)一管理版本號(hào)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • java Swing實(shí)現(xiàn)五子棋游戲

    java Swing實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java Swing實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評(píng)論

岳西县| 玉龙| 柳河县| 缙云县| 天峻县| 扎囊县| 额济纳旗| 子长县| 成武县| 琼结县| 恭城| 九江县| 兴海县| 定襄县| 平利县| 西乌| 通河县| 溧水县| 上犹县| 西林县| 蒲城县| 大兴区| 景东| 乐至县| 海门市| 师宗县| 庆城县| 昭平县| 柏乡县| 长子县| 阿拉尔市| 泾川县| 包头市| 张家界市| 舞阳县| 万荣县| 邹平县| 祁门县| 遂溪县| 金华市| 乌兰察布市|