📄 dateconverter.java
字号:
/**
* DateConverter.java
* @version 1.0.1
*/
package tools.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateConverter
{
/**
* Define SimpleDateFormat variable to return the formatted string according to it
*/
static public SimpleDateFormat MM_dd_yy = new SimpleDateFormat("MM-dd-yy");
static public SimpleDateFormat h_mm_a = new SimpleDateFormat("h:mm a");
static public SimpleDateFormat MM_DD_YYYY_HH_MM_SS = new SimpleDateFormat("MM_dd_yyyy_hh_mm_ss");
static public SimpleDateFormat MMddyyyy = new SimpleDateFormat("MM/dd/yyyy");
static public SimpleDateFormat MM_DD_YY = new SimpleDateFormat("MM_dd_yy");
static public SimpleDateFormat MMddyy = new SimpleDateFormat("MM/dd/yy");
static public SimpleDateFormat HHmmss = new SimpleDateFormat("HH:mm:ss");
static public SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
static public SimpleDateFormat MMdotDDdotYYYY = new SimpleDateFormat("MM.dd.yyyy");
static public SimpleDateFormat MMddYYYY_HHmmss = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
static public SimpleDateFormat YYYYMMdd_HHmmss = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
static public SimpleDateFormat YYYYMMdd = new SimpleDateFormat("yyyy.MM.dd");
static public SimpleDateFormat YYYY_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
static String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static public String getSimpleMonthName(int month)
{
return months[month];
}
static public String get_MM_dd_yy(java.util.Date date)
{
if(date == null) return "null";
return MM_dd_yy.format(date);
}
static public String get_h_mm_a(java.util.Date date)
{
if(date == null) return "null";
return h_mm_a.format(date);
}
static public String get_MM_DD_YYYY_HH_MM_SS(java.util.Date date)
{
if(date==null) return "null";
return MM_DD_YYYY_HH_MM_SS.format(date);
}
static public String get_MMddyyyy(java.util.Date date)
{
if(date==null) return "null";
return MMddyyyy.format(date);
}
static public java.util.Date parse_MM_DD_YYYY(String strDate)
{
if(strDate==null) return null;
try{
return MMddyyyy.parse(strDate);
}catch(java.text.ParseException pe){
return null;
}
}
static public String get_MM_DD_YY(java.util.Date date)
{
if(date==null) return "null";
return MM_DD_YY.format(date);
}
static public String get_MMddyy(java.util.Date date)
{
if(date==null) return "null";
return MMddyy.format(date);
}
static public String get_YYYYMMdd(java.util.Date date)
{
if(date==null) return "null";
return YYYYMMdd.format(date);
}
static public String get_YYYY_MM_dd(java.util.Date date)
{
if(date==null)
return "N/A";
return YYYY_MM_dd.format(date);
}
static public String get_YYYYMMdd_HHmmss(java.util.Date date)
{
if(date==null) return "null";
return YYYYMMdd_HHmmss.format(date);
}
static public String get_MMddYYYY_HHmmss(java.util.Date date)
{
if(date==null) return "NULL";
return MMddYYYY_HHmmss.format(date);
}
static public String get_MMdotDDdotYYYY(java.util.Date date)
{
if(date==null) return "NULL";
return MMdotDDdotYYYY.format(date);
}
static public String get_HHmmss(java.util.Date date)
{
if(date==null) return "null";
return HHmmss.format(date);
}
static public String get_HHmm(java.util.Date date)
{
if(date==null) return "null";
return HHmm.format(date);
}
static public java.util.Date getPrevMonthDate(java.util.Date date, int nMonth)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = nMonth * (-1);
cal.add(Calendar.MONTH, month);
return cal.getTime();
}
static public java.util.Date getNextMonthDate(java.util.Date date, int nMonth)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, nMonth);
return cal.getTime();
}
static public java.util.Date getPrevDay(java.util.Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
static public java.util.Date getNextDay(java.util.Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
return cal.getTime();
}
static public java.util.Date getNextDays(java.util.Date date, int nDates)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, nDates);
return cal.getTime();
}
static public java.util.Date getMidnight(java.util.Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
static public java.util.Date getNextHour(java.util.Date date, int nHour)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, nHour);
return cal.getTime();
}
static public java.util.Date getNext15Min(java.util.Date date, int n15Min)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, n15Min*15);
return cal.getTime();
}
static public java.util.Date getNext5Min(java.util.Date date, int n5Min)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, n5Min*5);
return cal.getTime();
}
static public java.util.Date getNextSecond(java.util.Date date, int nSecond)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.SECOND, nSecond);
return cal.getTime();
}
static public int getHourOfDay(java.util.Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY);
}
static public int get15MinOfDay(java.util.Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hours = cal.get(Calendar.HOUR_OF_DAY);
int mins = cal.get(Calendar.MINUTE);
return (hours*4 + mins/15);
}
static public int get5MinOfDay(java.util.Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hours = cal.get(Calendar.HOUR_OF_DAY);
int mins = cal.get(Calendar.MINUTE);
return (hours*12 + mins/5);
}
/**
* months between oldDate & curDate
* oldDate: 06/25/00
* curDate: 07/24/00<1> 07/25/00<2>; 07/26/00<2>
* oldDate: 12/25/99
* curDate: 01/24/00<1> 01/25/00<2>; 01/26/00<2>
*/
static public int getMonths(java.util.Date curDate, java.util.Date oldDate)
{
Calendar cur = Calendar.getInstance();
cur.setTime(curDate);
Calendar old = Calendar.getInstance();
old.setTime(oldDate);
if(cur.before(old)) return 0;
boolean done = false;
int counter = 0;
while(true)
{
if(old.after(cur)) return counter;
old.add(Calendar.MONTH, 1);
counter++;
}
}
/**
* months between oldDate & curDate
* oldDate: 06/25/00
* curDate: 6/26/00<1> 7/1/00<6>
*/
static public int getDays(java.util.Date curDate, java.util.Date oldDate)
{
Calendar cur = Calendar.getInstance();
cur.setTime(curDate);
Calendar old = Calendar.getInstance();
old.setTime(oldDate);
if(cur.before(old)) return 0;
boolean done = false;
int counter = 0;
while(true)
{
if(old.after(cur) || old.equals(cur)) return counter;
old.add(Calendar.DATE, 1);
counter++;
}
}
/**
* weekNum: 1-Monday ... 7-Sunday
* relative to today (passed)
*/
static public java.util.Date getLastWeekDate(int weekNum, java.util.Date today)
{
// WeekNum : 1-Monday ... 7-Sunday
Calendar calendar=Calendar.getInstance();
calendar.setTime(today);
calendar.add(Calendar.DATE,-7-(calendar.get(Calendar.DAY_OF_WEEK)-2)+(weekNum-1));
return calendar.getTime();
}
/**
* compare if two dates are the same day
*/
static public boolean isDateEqualToDay(java.util.Date date1, java.util.Date date2)
{
Calendar calendar1=Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2=Calendar.getInstance();
calendar2.setTime(date2);
if(calendar1.get(Calendar.YEAR)==calendar2.get(Calendar.YEAR) &&
calendar1.get(Calendar.MONTH)==calendar2.get(Calendar.MONTH) &&
calendar1.get(Calendar.DATE)==calendar2.get(Calendar.DATE))
return true;
return false;
}
/**
* check if date is week day
*/
static public boolean isWeekDay(java.util.Date date)
{
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DAY_OF_WEEK);
if(day==Calendar.SATURDAY || day==Calendar.SUNDAY)
return false;
return true;
}
/**
* getDateByNumeric
*/
static public java.util.Date getDateByNumeric(long milliseconds)
{
return new java.util.Date(milliseconds);
}
/**
* getNumericByDate
*/
static public long getNumericByDate(java.util.Date date)
{
return date.getTime();
}
/**
* weekNum: 1-Monday ... 7-Sunday
* relative to today (passed)
*/
static public java.util.Date getEndDayOfTheMonth(java.util.Date today)
{
Calendar calendar=Calendar.getInstance();
calendar.setTime(today);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, 1);
calendar.add(Calendar.DATE, -1);
return calendar.getTime();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -