DateUtil time tool and dateutil Tool

Source: Internet
Author: User

DateUtil time tool and dateutil Tool

Package com. ruisi. core. utils;

Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;
Import java. util. GregorianCalendar;

Public final class DateUtil {

/** Default year, month, and day */
Public static final String defaultPattern = "yyyy-MM-dd ";

/** Hour12HMSPattern: year, month, day, hour, minute, second, 12-hour */
Public static final String hour12HMSPattern = "yyyy-MM-dd hh: mm: ss ";

/** Hour12HMPattern, in 12-hour format */
Public static final String hour12HMPattern = "yyyy-MM-dd hh: mm ";

/** Hour12HPattern, in 12-hour format */
Public static final String hour12HPattern = "yyyy-MM-dd hh ";

/** Hour24HMSPattern: year, month, day, hour, minute, second, 24-hour format */
Public static final String hour24HMSPattern = "yyyy-MM-dd HH: mm: ss ";

/** Hour24HMPattern, in 24-hour format */
Public static final String hour24HMPattern = "yyyy-MM-dd HH: mm ";

/** Hour24HPattern in 24-hour format */
Public static final String hour24HPattern = "yyyy-MM-dd HH ";

Public static String daysBetween (Date startDate, Date endDate ){
Float d = endDate. getTime ()-startDate. getTime ();
Float dd = d/86400000f;
Int ddd = (int) dd;

Float hh = (dd-ddd) * 24;
Int hhh = (int) hh;

Float mm = (hh-hhh) * 60;
Int mmm = (int) mm;

Float ss = (mm-mmm) * 60;
Int sss = (int) ss;
Return ddd + "day" + hhh + "Hour" + mmm + "Minute" + sss + "second ";
}

/**
* Returns the current date string of the preset Format.
*/
Public static String getToday (){
Date today = new Date ();
Return format (today );
}

Public static String getYestoday (){
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (new Date ());
Cal1.add (Calendar. DATE,-1 );
Return format (cal1.getTime ());
}

Public static String getTheDayBeforeYestoday (){
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (new Date ());
Cal1.add (Calendar. DATE,-2 );
Return format (cal1.getTime ());
}

Public static String getpreviusday3 (){
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (new Date ());
Cal1.add (Calendar. DATE,-3 );
Return format (cal1.getTime ());
}

Public static String getpreviusday4 (){
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (new Date ());
Cal1.add (Calendar. DATE,-4 );
Return format (cal1.getTime ());
}

/**
* Format Date into a string using the preset Format
*/
Public static String format (Date date ){
Return format (date, defaultPattern );
}

/**
* Format Date to a string using the Format parameter.
*/
Public static String format (Date date, String pattern ){
String returnValue = "";

If (date! = Null ){
SimpleDateFormat df = new SimpleDateFormat (pattern );
ReturnValue = df. format (date );
}

Return (returnValue );
}

/**
* Convert the string to Date using the preset format
*/
Public static Date parse (String strDate) throws ParseException {
Return parse (strDate, defaultPattern );
}

Public static Date parse (String strDate, String pattern)
Throws ParseException {
SimpleDateFormat df = new SimpleDateFormat (pattern );
Return df. parse (strDate );
}

/**
* Increase the number of whole months on the date
*/
Public static Date addMonth (Date date, int n ){
Calendar cal = Calendar. getInstance ();
Cal. setTime (date );
Cal. add (Calendar. MONTH, n );
Return cal. getTime ();
}

/**
* Add a day to the date
*/
Public static Date addDay (Date date, int n ){
Calendar cal = Calendar. getInstance ();
Cal. setTime (date );
Cal. add (Calendar. DATE, n );
Return cal. getTime ();
}

Public static String formatDateTime (Date date ){
SimpleDateFormat outFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Return outFormat. format (date );
}

@ SuppressWarnings ("static-access ")
Public static String getEndOfMonth (String year, String month ){
Calendar cal = Calendar. getInstance ();
Cal. set (cal. YEAR, Integer. parseInt (year ));
Cal. set (cal. MONTH, Integer. parseInt (month)-1 );
Return cal. getActualMaximum (cal. DAY_OF_MONTH) + "";
}

Public static String addDays (String sdate, int n) throws ParseException
{
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (parse (sdate, defaultPattern ));
Cal1.add (Calendar. DATE, n );
Return format (cal1.getTime ());

}

Public static String getFirstOfMonth () throws ParseException {
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (new Date ());
Cal1.set (5, 1 );
Return format (cal1.getTime ());

}

Public static String getFirstOfMonth (String sDate) throws ParseException {
Calendar cal1 = Calendar. getInstance ();
Cal1.setTime (parse (sDate, defaultPattern ));
Cal1.set (5, 1 );
Return format (cal1.getTime ());

}

/**
* Obtain the year
*
* @ Param sdate
* @ Return String
*/
Public static String getYear (String sdate ){
String [] date = sdate. split ("-");
Return date [0];
}

/**
* Obtain the month
*
* @ Param sdate
* @ Return String
*/
Public static String getMonth (String sdate ){
String [] date = sdate. split ("-");
Return date [1];
}

Public static String getCurrentYear (){
Calendar cale = Calendar. getInstance ();
Return Integer. toString (cale. get (Calendar. YEAR ));
}

Public static String getCurrentMonth (){
Calendar cale = Calendar. getInstance ();
Int month = cale. get (Calendar. MONTH );
Month ++;
String sMonth = Integer. toString (month );
If (month <10)
SMonth = "0" + month;
Return sMonth;
}

/**
* Get days
*
* @ Param sdate
* @ Return String
*/
Public static String getDay (String sdate ){
String [] date = sdate. split ("-");
Return date [2];
}

Public static String getFullDate (String date ){
If (date! = Null & date. length () = 1)
Return "0" + date;
Return date;
}

Public static String getSimpleDateString (String sdate ){
Return sdate. replace ("-","");
}

// Convert a date from a character to a date type
Public static Date convertStringToDate (String pattern, String strDate)
Throws ParseException {
Date aDate = null;
ADate = parse (strDate, pattern );
Return aDate;
}

// Obtain the string of the current date according to the specified format
Public static String getTodayDate (String aMask ){
Date date = new Date ();
Return getDateTime (aMask, date );
}

// Obtain the string of the specified date based on the specified format
Public static String getDateTime (String aMask, Date aDate ){
SimpleDateFormat df = null;
String returnValue = "";
Df = new SimpleDateFormat (aMask );
ReturnValue = df. format (aDate );
Return (returnValue );
}

Public static int getYear (){
Calendar c = Calendar. getInstance ();
Int yy = c. get (Calendar. YEAR );
Return yy;
}

Public static int getMonth (){
Calendar c = Calendar. getInstance ();
Int month = c. get (Calendar. MONTH );
Return month + 1;
}

Public static int getDate (){
Calendar c = Calendar. getInstance ();
Int date = c. get (Calendar. DATE );
Return date;
}

Public static int getHour (){
Calendar c = Calendar. getInstance ();
Int hour = c. get (Calendar. HOUR_OF_DAY );
Return hour;
}

Public static int getSecond (){
Calendar c = Calendar. getInstance ();
Int second = c. get (Calendar. SECOND );
Return second;
}

Public static int getMinute (){
Calendar c = Calendar. getInstance ();
Int minute = c. get (Calendar. MINUTE );
Return minute;
}

Public static String getCurDateTime (){
Return getCurDate () + "" + getCurTime ();
}

Public static String getCurDate (){
String fullDate = getCurYearMonth ();
Int temp = getDate ();
If (temp <10 ){
FullDate + = "-0" + temp;
} Else {
FullDate + = "-" + temp;
}
Return fullDate;
}

Public static String getCurYearMonth (){
String fullDate = String. valueOf (getYear ());
Int temp = getMonth ();
If (temp <10 ){
FullDate + = "-0" + temp;
} Else {
FullDate + = "-" + temp;
}
Return fullDate;
}

Public static String getCurTime (){
String time = getCurHourMinute ();
Int temp = getSecond ();
If (temp <10 ){
Time + = ": 0" + temp;
} Else {
Time + = ":" + temp;
}
Return time;
}

Public static String getCurHourMinute (){
String time;
Int temp = getHour ();
If (temp <10 ){
Time = "0" + temp + ":";
} Else {
Time = temp + ":";
}
Temp = getMinute ();
If (temp <10 ){
Time + = "0" + temp;
} Else {
Time + = temp;
}
Return time;
}
/**
*
* @ Description: obtains the current time.
* @ Param @ return
* @ Return StringBuffer
* @ Throws
* @ Author ningpeng
* @ Date January 1, January 8, 2016
*/
Public static StringBuffer getNow ()
{
Date date = new Date ();
Calendar c = new GregorianCalendar ();
C. setTime (date );
Int year = c. get (Calendar. YEAR );
Int month = c. get (Calendar. MONTH) + 1;
Int day = c. get (Calendar. DATE );
Int hour = c. get (Calendar. HOUR_OF_DAY );
Int minute = c. get (Calendar. MINUTE );
Int seconds = c. get (Calendar. SECOND );
StringBuffer buff = new StringBuffer ();
Buff. append (year). append (month). append (day). append (hour). append (minute). append (seconds). toString ();
Return buff;
}

Public static String DateFormat (Date date ){
Calendar c = Calendar. getInstance ();
C. setTime (date );
C. get (java. util. Calendar. HOUR_OF_DAY );
String pattern = "yyyy-MM-dd ";
If (c. get (java. util. Calendar. HOUR_OF_DAY )! = 0 ){
Pattern = "yyyy-MM-dd hh ";
} Else if (c. get (java. util. Calendar. MINUTE )! = 0 ){
Pattern = "yyyy-MM-dd hh: mm ";
} Else if (c. get (java. util. Calendar. SECOND )! = 0 ){
Pattern = "yyyy-MM-dd hh: mm: ss ";
}
Return new SimpleDateFormat (pattern). format (date );
}

Public static void main (String [] args ){
System. out. println (getMonth ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.