java Date 상용 날짜 기능 통합

5038 단어
package com.yao.util.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static String defaultSimpleFormater = "yyyy-MM-dd hh:mm:ss";
/**
*          
* 
* @return
*/
public static String getDefaultSimpleFormater() {
   return defaultSimpleFormater;
}
/**
*              
* 
* @param defaultFormatString
*/
public static void setDefaultSimpleFormater(String defaultFormatString) {
   DateUtil.defaultSimpleFormater = defaultFormatString;
}
/**
*      
* 
* @param date
* @param formatString
* @return
*/
public static String format(Date date, String formatString) {
   SimpleDateFormat df = new SimpleDateFormat(formatString);
   return df.format(date);
}
/**
*      (      )
* 
* @param date
* @return
*/
public static String format(Date date) {
   return format(date, defaultSimpleFormater);
}
/**
*      
* 
* @param dateString
* @param formatString
* @return
*/
public static Date parse(String dateString, String formatString) {
   SimpleDateFormat df = new SimpleDateFormat(formatString);
   try {
    return df.parse(dateString);
   } catch (ParseException e) {
    return null;
   }
}
/**
*      (      )
* 
* @param dateString
* @return
*/
public static Date parse(String dateString) {
   return parse(dateString, defaultSimpleFormater);
}
/**
*   
* 
* @return
*/
public static Date yesterday() {
   return addDay(-1);
}
/**
*   
* 
* @return
*/
public static Date tomorrow() {
   return addDay(1);
}
/**
*   
* 
* @return
*/
public static Date now() {
   return new Date(System.currentTimeMillis());
}
/**
*    
* 
* @param value
* @return
*/
public static Date addDay(int value) {
   Calendar now = Calendar.getInstance();
   now.add(Calendar.DAY_OF_YEAR, value);
   return now.getTime();
}
/**
*    ,    
* 
* @param date
* @param value
* @return
*/
public static Date addDay(Date date, int value) {
   Calendar now = Calendar.getInstance();
   now.setTime(date);
   now.add(Calendar.DAY_OF_YEAR, value);
   return now.getTime();
}
/**
*    
* 
* @param value
* @return
*/
public static Date addMonth(int value) {
   Calendar now = Calendar.getInstance();
   now.add(Calendar.MONTH, value);
   return now.getTime();
}
/**
*    ,    
* 
* @param date
* @param value
* @return
*/
public static Date addMonth(Date date, int value) {
   Calendar now = Calendar.getInstance();
   now.setTime(date);
   now.add(Calendar.MONTH, value);
   return now.getTime();
}
/**
*    
* 
* @param value
* @return
*/
public static Date addYear(int value) {
   Calendar now = Calendar.getInstance();
   now.add(Calendar.YEAR, value);
   return now.getTime();
}
/**
*    ,    
* 
* @param date
* @param value
* @return
*/
public static Date addYear(Date date, int value) {
   Calendar now = Calendar.getInstance();
   now.setTime(date);
   now.add(Calendar.YEAR, value);
   return now.getTime();
}
/**
*     
* 
* @param value
* @return
*/
public static Date addHour(int value) {
   Calendar now = Calendar.getInstance();
   now.add(Calendar.HOUR_OF_DAY, value);
   return now.getTime();
}
/**
*     ,    
* 
* @param date
* @param value
* @return
*/
public static Date addHour(Date date, int value) {
   Calendar now = Calendar.getInstance();
   now.setTime(date);
   now.add(Calendar.HOUR_OF_DAY, value);
   return now.getTime();
}
/**
*     
* 
* @param value
* @return
*/
public static Date addMinute(int value) {
   Calendar now = Calendar.getInstance();
   now.add(Calendar.MINUTE, value);
   return now.getTime();
}
/**
*     ,    
* 
* @param date
* @param value
* @return
*/
public static Date addMinute(Date date, int value) {
   Calendar now = Calendar.getInstance();
   now.setTime(date);
   now.add(Calendar.MINUTE, value);
   return now.getTime();
}
/**
*   
* 
* @return
*/
public static int year() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.YEAR);
}
/**
*   
* 
* @return
*/
public static int month() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.MONTH);
}
/**
*  ( )
* 
* @return
*/
public static int day() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.DAY_OF_MONTH);
}
/**
*   ( )
* 
* @return
*/
public static int hour() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.HOUR);
}
/**
*   
* 
* @return
*/
public static int minute() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.MINUTE);
}
/**
*  
* 
* @return
*/
public static int second() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.SECOND);
}
/**
*    (   )
* 
* @return
*/
public static int weekday() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
*     ?
* 
* @return
*/
public static boolean isAm() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.AM_PM) == 0;
}
/**
*     ?
* 
* @return
*/
public static boolean isPm() {
   Calendar now = Calendar.getInstance();
   return now.get(Calendar.AM_PM) == 1;
}
}

좋은 웹페이지 즐겨찾기