Java Common date format and date Computing tools class __java

Source: Internet
Author: User
Tags date now dateformat

Package com.java.mvc.util;

Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.GregorianCalendar;
Import java.util.List;

public class Dateutils {

Public final static String format_yyyy_mm_dd = "YYYY-MM-DD";

Public final static String format_yyyy_mm_dd_hh_mm_ss = "Yyyy-mm-dd hh:mm:ss";

Public final static String FORMAT_YYYYMMDD = "YYYYMMDD";

Public final static String format_yyyymm = "Yyyymm";

Public final static String format_yyyy = "YYYY";

Public final static String Format_yyyymmddhhmiss = "YyyyMMdd HH:mm:ss";

Public final static String Format_yyyymmddhhmmss = "YYYYMMDDHHMMSS";

Public final static String format_yyyy_mm_dd_hh_mi_ss = "Yyyy-mm-dd HH:mm:ss";

Public final static String format_yyyy_mm_dd_hh_mi_ss_sss = "Yyyy-mm-dd HH:mm:ss:SSS";

Public final static String format_yyyy_mm = "yyyy-mm";

Public final static String format_dd_hh_mm_ss = "DD HH:mm:ss";

Public final static String format_hh_mm_ss = "HH:mm:ss";

public static final int use_year = 1;

public static final int use_month = 2;
public static final int use_day = 3; /** * Initializes the date format object according to the corresponding format * @param pattern * @return/public static DateFormat GetdatefoRmat (final String pattern) {Return to New SimpleDateFormat (pattern);}/** * Gets the date from the current date of Nth days, if the days>0 is the current date after a few days, otherwise a few days before * @param days * @return/public static Date Getpreviousornextdaysofnow (int.) {return GETPREVIOUSORNEXTDAYSO
FDate (New Date (), days); /** * Returns the current date in the format of: Format (YYYY-MM-DD) * @return/public static String Getcurrentdate () {Date now = new Date ()
    ;
Return GetDateFormat (FORMAT_YYYY_MM_DD). FORMAT (now); /** * Returns the specified date and the specified format date String * @return/public static string getspecifydate (date date,string pattern) {return GETDA
Teformat (pattern). Format (date);  /** * Returns the current date in the format of: Format (yyyy-mm-dd HH:MM:SS) * @return/public static String GetCurrentDateTime () {Date now
    = new Date ();
Return GetDateFormat (FORMAT_YYYY_MM_DD_HH_MM_SS). FORMAT (now); }/** * Current date with format (YYYY-MM-DD HH:MM:SS:SSS) * @return/public static String Getcurrentdatetimemillisec Ond (date date) {return GetDateFormat (FORMAT_YYYY_MM_DD_HH_MI_SS_SSS). Format (date); /** * Converts to Date * @return/public static date Getdatebytimemillisecond according to the corresponding time format (YYYY-MM-DD HH:MM:SS:SSS) string (String da
    TE) throws parseexception{simpledateformat sdf = new SimpleDateFormat (FORMAT_YYYY_MM_DD_HH_MI_SS_SSS);
return Sdf.parse (date); /** * Converts to date * @return/public static date Getdatebypattern (string date,string pattern) throws Parseex according to the corresponding time format string
    ception{SimpleDateFormat sdf = new SimpleDateFormat (pattern);
return Sdf.parse (date); /** * Get the first day of the month * @return * * * public static Date Getfirstdayofthismonth () {Calendar nowday = calendar.getinstance
    ();
    Nowday.set (Calendar.day_of_month, 1);
    Nowday.set (calendar.hour_of_day, 0);
    Nowday.set (calendar.minute, 0);
    Nowday.set (Calendar.second, 0);
    Nowday.set (Calendar.millisecond, 0);
return Nowday.gettime ();
    /** * Get the current date of the last month date * @return * * * public static date Getlastmonth () {Calendar nowday = calendar.getinstance ();
    Nowday.settime (New Date ()); NowDay.set (Calendar.day_of_month, 1);
    Nowday.add (Calendar.month,-1);
return Nowday.gettime (); public static Date Getlastdaybycalendar (Calendar calendar) {Calendar.set (Calendar.day_of_month, Calendar.getactua
    Lmaximum (Calendar.day_of_month));
return Calendar.gettime (); /** * To determine whether the specified date is within the current N-day * @param old * @param days * @return/public static Boolean isdaysinterval (date old, int
    Days) {The Calendar now = Calendar.getinstance ();
Return (Now.gettimeinmillis ()-Old.gettime ()) <= days * 24 * 3600 * 1000;
    /** * Get the first day of the corresponding month according to calendar * @param calendar * @return/public static Date getfirstdayofthemonth (Calendar calendar) {
    Calendar.set (Calendar.day_of_month, 1);
    Calendar.set (calendar.hour_of_day, 0);
    Calendar.set (calendar.minute, 0);
    Calendar.set (Calendar.second, 0);
    Calendar.set (Calendar.millisecond, 0);
return Calendar.gettime (); /** * Get the date before or after the specified date * @param days * @return/public static date GetpreviousornextdaysoFDate (date date, int days) {Calendar calendar = calendar.getinstance ();
    Calendar.settime (date);
    Calendar.add (Calendar.day_of_year, days);
return Calendar.gettime (); /** * Gets the same date as the number of months from the specified date * @param date * @param month * @return */public static date Getpreviousornextmonthsofdate (date date, int month)
    {Calendar calendar = calendar.getinstance ();
    Calendar.settime (date);
    Calendar.add (Calendar.month, MONTH);
return Calendar.gettime (); /** * Gets the same date as the number of years between the specified dates * @param date * @param month * @return * * * public static date getpreviousornextyearsofdate (D
    Ate date, int year) {Calendar calendar = calendar.getinstance ();
    Calendar.settime (date);
    Calendar.add (Calendar.year, year);
return Calendar.gettime (); 
/** * Gets the start time of the current date * @return/public static date Getbeginofcurrentday () {return getbeginofthedate (new date ()); /** * Gets the end time of the current date * @return/public static date Getendofcurrentday () {return getendofthedate (newDate ()); public static long Getdifferencedays (Calendar begincalender, calendar Endcalendar) {Long days = (Endcalendar.getti
    Meinmillis ()-Begincalender.gettimeinmillis ())/(24 * 60 * 60 * 1000);
    Days = days + 1;
return days; /** * Get the first day of the current year * @return * * * public static Date getbeginofcurrentyear () {Calendar calendar = Calendar.getinsta
    NCE ();
    Calendar.set (calendar.month, 0);
    Calendar.set (Calendar.day_of_month, 1);
    Calendar.set (calendar.hour_of_day, 0);
    Calendar.set (calendar.minute, 0);
    Calendar.set (Calendar.second, 0);
    Calendar.set (Calendar.millisecond, 0);
return Calendar.gettime (); /** * Get the current time before and after n minutes * @param date * @param minute * @return * * public static date getpreviousornextminutesofdate (D
    Ate date, int minute) {Calendar now = calendar.getinstance ();
    Now.settime (date);
    Now.add (Calendar.minute, MINUTE);
return Now.gettime (); /** * Gets the last moment of the specified month * @param date * @return/public static date GetLastDateofthemonth (date date) {Calendar NewDay = calendar.getinstance ();
    Newday.settime (date);
    Newday.set (Calendar.day_of_month, Newday.getactualmaximum (Calendar.day_of_month));
    Newday.set (Calendar.hour_of_day, 23);
    Newday.set (Calendar.minute, 59);
    Newday.set (Calendar.second, 59);
    Newday.set (Calendar.millisecond, 999);
return Newday.gettime ();
    public static date getbeginofthedate (date date) {Calendar nowday = calendar.getinstance ();
    Nowday.settime (date);
    Nowday.set (calendar.hour_of_day, 0);
    Nowday.set (calendar.minute, 0);
    Nowday.set (Calendar.second, 0);
    Nowday.set (Calendar.millisecond, 0);
return Nowday.gettime ();
    public static date getendofthedate (date date) {Calendar nowday = calendar.getinstance ();
    Nowday.settime (date);
    Nowday.set (Calendar.hour_of_day, 23);
    Nowday.set (Calendar.minute, 59);
    Nowday.set (Calendar.second, 59); Nowday.set (Calendar.millisecond, 998);//sql Server BUG return nowday. GetTime (); /** * Gets all the days set for the specified time period * @param begindate * @param enddate * @return * @throws Exception/public static list< String> getareatime (String begindate, String enddate) throws Exception {list<string> timelist = new Arraylis
    T<string> (); if (EndDate = null | |
        "". Equals (EndDate)) {Timelist.add (begindate);
    return timelist;
        }//day if (Begindate.length () <= 7) {return Getareamonthtime (begindate, EndDate);
    Month} else {return getareadaytime (begindate, EndDate); }} private static list<string> Getareamonthtime (String beginmonth, String endmonth) throws Exception {Date PA
    Rsedbeginmonth = GetDateFormat (format_yyyy_mm). Parse (Beginmonth);
    String tempstr = "";
    list<string> timelist = new arraylist<string> ();
    int months = 0; while (!endmonth.equals (TEMPSTR)) {TempStr = GetDateFormat (format_yyyy_mm). FORMAT (Getpreviousornextmonthsofdate (p ARsedbeginmonth, months));
        Timelist.add (TEMPSTR);
    months++;
return timelist; /** * Gets all the days set for the specified time period * @param begindate * @param enddate * @return * @throws Exception/public static list< String> getareadaytime (String begindate, String enddate) throws Exception {Date parsedbeginmonth = GetDateFormat (F
    ORMAT_YYYY_MM_DD). Parse (begindate);
    String tempstr = "";
    list<string> timelist = new arraylist<string> ();
    int months = 0; while (!enddate.equals (TEMPSTR)) {TempStr = GetDateFormat (FORMAT_YYYY_MM_DD). FORMAT (Getpreviousornextdaysofdate (p
        Arsedbeginmonth, months));
        Timelist.add (TEMPSTR);
    months++;
return timelist; /** * Gets the long form of the corresponding date before the current date, such as 20160227 * @return/public static Long Getpreviousday () {Date date = Getpreviouso
    Rnextdaysofdate (New Date (),-1);
Return long.valueof (GetDateFormat (FORMAT_YYYYMMDD). FORMAT (date));
/** * Gets the long form of the corresponding date before the specified date, such as 20160227 * @return */public static Long getpreviousday (date date,int days) {Date dates = Getpreviousornextdaysofdate (date, days);
Return long.valueof (GetDateFormat (FORMAT_YYYYMMDD). FORMAT (dates)); /** * Gets the same date string * @param date * @param n * @return * @throws Exception/public static string GETPR the specified date n months or after n months
    Eviousornextmonthsofthemonth (String date, int n) throws Exception {Calendar calendar = new GregorianCalendar ();
    DateFormat DateFormat = GetDateFormat (FORMAT_YYYYMM);
    Calendar.settime (Dateformat.parse (date));
    Calendar.add (Calendar.month, N);
Return Dateformat.format (Calendar.gettime ()); public static string Getpreviousornextdaysoftheday (String date, int days) throws Exception {Calendar calendar = N
    EW GregorianCalendar ();
    DateFormat DateFormat = GetDateFormat (FORMAT_YYYYMMDD);
    Calendar.settime (Dateformat.parse (date));
    Calendar.add (Calendar.day_of_month, days);
Return Dateformat.format (Calendar.gettime ()); public static Long GetFirstDayofcurrentmonth () {Date date = Getfirstdayofthismonth ();
    String datestr = GetDateFormat (FORMAT_YYYYMMDD). FORMAT (date);
Return long.valueof (DATESTR);
    public static Long Getfirstmonthofnow () {Calendar calendar = calendar.getinstance ();
    Calendar.set (Calendar.day_of_month, 1);
    Calendar.set (calendar.month, 0);
Return long.valueof (GetDateFormat (format_yyyymm). FORMAT (Calendar.gettime ()));
    public static Integer Getcurrentday () {Date date = new Date ();
Return Integer.parseint (GetDateFormat (FORMAT_YYYYMMDD). FORMAT (date));
    public static Integer Getcurrentmonth () {Date date = new Date ();
Return Integer.parseint (GetDateFormat (format_yyyymm). FORMAT (date));
    public static Integer Getcurrentyear () {Date date = new Date ();
Return Integer.parseint (GetDateFormat (format_yyyy). FORMAT (date));
    public static Integer Getkpidbanyoffsetdayofnow (int day) {Calendar nowday = calendar.getinstance (); Nowday.set (Calendar.day_of_year, nowDay.get (calendar.day_of_year) + day);
Return Integer.parseint (GetDateFormat (FORMAT_YYYYMMDD). FORMAT (Nowday.gettime ())); /** * Get current date last month * @return * * public static Long Getpreviousmonthofnextyear () {Calendar nowday = calendar.ge
    Tinstance ();
    Nowday.settime (New Date ());
    Nowday.set (Calendar.day_of_month, 1);
    Nowday.add (Calendar.year, + 1);
    Nowday.add (Calendar.month,-1);
    Long time = long.valueof (GetDateFormat (format_yyyymm). FORMAT (Nowday.gettime ()));
return time; /** * Get the same date as the next month of the specified date * @param date * @return/public static Long Getthesamedayofthenextmonth (date date) {Dat
    E next = getpreviousornextmonthsofdate (date, 1);
    Long time = long.valueof (GetDateFormat (FORMAT_YYYYMMDD). FORMAT (next);
return time; /** * Get the same date for the current date next month * @param date * @return/public static Long Getthesamedayofnextmonth () {return Getthesa
Medayofthenextmonth (New Date ()); public static Long Getpreviousyear () {Calendar nowday = Calendar.getInstance ();
    Nowday.settime (New Date ());
    Nowday.add (Calendar.year,-1);
Return long.valueof (GetDateFormat (format_yyyy). FORMAT (Nowday.gettime ())); public static list<string> Gettimelistbyparameter (Date paramdate, int paramter, int type) {if (type = = Use_y
        EAR) {Date newdate = getpreviousofnextyearofdate (paramdate,-paramter);
    Return Getdaysbetweendate (Paramdate, newdate);
        else if (type = = Use_month) {Date newdate = getpreviousornextmonthsofdate (paramdate, paramter);
    Return Getdaysbetweendate (Paramdate, newdate);
    else {return getdaysbybegindate (paramdate, paramter);
    The private static Date getpreviousofnextyearofdate (date date, int year) {The Calendar now = Calendar.getinstance ();
    Now.settime (date);
    Now.add (calendar.year,year);
return Now.gettime (); private static list<string> getdaysbetweendate (date paramdate, date newdate) {DateFormat DateFormat = Getdat Eformat (FORMAT_YYYYMMDD);
    String formatnewdate = Dateformat.format (newdate);
    String tempstr = "";
    list<string> timelist = new arraylist<string> ();
    int days = 0; while (!formatnewdate.equals (TEMPSTR)) {TempStr = Dateformat.format (Getpreviousornextdaysofdate (paramDate,-days)
        );
        Timelist.add (TEMPSTR);
    days++;
return timelist; private static list<string> Getdaysbybegindate (Date paramdate, int paramter) {list<string> timelist =
    New Arraylist<string> (); for (int i = 1; I <= paramter i++) {Timelist.add (GetDateFormat (FORMAT_YYYYMMDD). FORMAT (Getpreviousornextdayso
    FDate (Paramdate, i));
return timelist;
    public static Long Getfirstmonthofcurrentyear () {Calendar calendar = calendar.getinstance ();
    Calendar.set (calendar.month,0);
Return long.valueof (GetDateFormat (format_yyyymm). FORMAT (Calendar.gettime ()));
 public static date Getpreviousornextmonthsofdatetime (date date, int months) {   Calendar calendar = Calendar.getinstance ();
    Calendar.settime (date);
    Calendar.add (calendar.month, months);
return Calendar.gettime (); /** * Gets the date string of N days before and after the specified date * @param ymd * @param day * @return * @throws parseexception/public static string GE Tpreviousornextdaysofnow (String ymd,int days) throws ParseException {Date date = GetDateFormat (FORMAT_YYYY_MM_DD). par
    SE (YMD);
    Date result = Getpreviousornextdaysofdate (date, days);
Return GetDateFormat (FORMAT_YYYY_MM_DD). FORMAT (result);
    public static Date getbeginofthepreviousornextmonths (int month) {Calendar calendar = calendar.getinstance ();
    Calendar.add (Calendar.month, MONTH);
    Calendar.set (Calendar.day_of_month, 1);
    Calendar.set (calendar.hour_of_day, 0);
    Calendar.set (calendar.minute, 0);
    Calendar.set (Calendar.second, 0);
    Calendar.set (Calendar.millisecond, 0);
return Calendar.gettime (); public static date getpreviousornextworkdaysofdate (date date, int days) {CalenDar Calendar = calendar.getinstance ();
    Calendar.settime (date);
    int step = 0;
    if (days>0) {step = 1;
    }else{step =-1; for (int i = 0; i < Math.Abs (days);)
        {Calendar.add (calendar.day_of_year, step); if (Calendar.get (calendar.day_of_week) ==calendar.saturday| |
        Calendar.get (Calendar.day_of_week) ==calendar.sunday) {continue;
    } i++;
return Calendar.gettime ();
    public static date Getbegindateofthemonth (date date) {Calendar NewDay = calendar.getinstance ();
    Newday.settime (date);
    Newday.set (Calendar.day_of_month, 1);
    Newday.set (calendar.hour_of_day, 0);
    Newday.set (calendar.minute, 0);
    Newday.set (Calendar.second, 0);
    Newday.set (Calendar.millisecond, 0);
return Newday.gettime ();
 }

public static void Main (string[] args) throws Exception {
System.out.println (Getpreviousmonthofnextyear ());
System.out.println (Getpreviousornextdaysofnow (3));
System.out.println (Gettimelistbyparameter (New Date (), 1, 3));
// }

}

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.