Java Time processing class

Source: Internet
Author: User
Tags dateformat parse string string format

/**

* Early in the day

*/

public static calendar startofday (Calendar c) {
if (c = = null) {
return C;
}
C.set (calendar.hour_of_day, 0);
C.set (calendar.minute, 0);
C.set (Calendar.second, 0);
C.set (Calendar.millisecond, 0);
return C;
}

/**

* The night of the day
* @param
* @return
*/

public static calendar endofday (Calendar c) {
if (c = = null) {
return C;
}
C.set (Calendar.hour_of_day, 23);
C.set (Calendar.minute, 59);
C.set (Calendar.second, 59);
C.set (Calendar.millisecond, 998);
return C;
}

public static Calendar Startofyear (Integer year) {
if (year = = null)
return null;
Calendar C = calendar.getinstance ();
C.set (Calendar.year, year);
C.set (Calendar.month, calendar.january);
C.set (Calendar.day_of_month, 1);
return Startofday (c);
}

public static Calendar Endofyear (Integer year) {
if (year = = null)
return null;
Calendar C = calendar.getinstance ();
C.set (Calendar.year, year);
C.set (Calendar.month, Calendar.december);
C.set (Calendar.day_of_month, C.getactualmaximum (Calendar.day_of_month));
return Endofday (c);
}

/**
* Converts the given string to a date and, if no parser is set, resolves it with the default format (YYYY-MM-DD HH:mm:ss)
*
* @param s
* Given string
* @param format
* Parse Format
* @return Date Object
* @throws ParseException
*/
public static Calendar String2calendar (String s, dateformat format) throws ParseException {
if (! Stringtools.hastext (s))
return null;
Calendar C = calendar.getinstance ();
Date Date = (format = = null?) Default_format:format). Parse (s);
C.settimeinmillis (Date.gettime ());
return C;
}

/**
* Converts the given string to a date and, if no parsing string is set, resolves it with the default format (YYYY-MM-DD HH:mm:ss)
*
* @param s
* Given string
* @param format
* Parse String
* @return Date Object
* @throws ParseException
*/
public static Calendar String2calendar (string s, string format) throws ParseException {
Return String2calendar (S, New SimpleDateFormat (Stringtools.hastext (format) format:default_pattern));
}

/**
* Converts a given string to a date in the default parsing format (YYYY-MM-DD HH:mm:ss)
*
* @param s
* @return
* @throws ParseException
*/
public static Calendar String2calendar (String s) throws ParseException {
Return String2calendar (S, Default_pattern);
}

/**
* Format the given date output, if no output format is set, the pattern format (yyyy-mm-dd HH:mm:ss) output is used
*
* @param c
* Date
* @param format
* Parse Format
* @return
*/
public static String calendar2string (Calendar C, dateformat format) {
if (c = = null)
return null;
return (format = = null?) Default_format:format). FORMAT (C.gettime ());
}

/**
* Format the given date output, if no output format is set, the pattern format (yyyy-mm-dd HH:mm:ss) output is used
*
* @param c
* Date
* @param format
* Parse Format
* @return
*/
public static string calendar2string (Calendar C, string format) {
Return Calendar2string (C, (stringtools.hastext (format)? New SimpleDateFormat (format): Default_format));
}

/**
* Formatted output for the given date in the default format (YYYY-MM-DD HH:mm:ss)
*
* @param c
* Date
* @return
*/
public static String calendar2string (Calendar c) {
Return calendar2string (c, Default_format);
}

/**
* Get the first and last day of one months
*
* @param monthstr
* @return [0] The first day of the month, [1] The last day of the month
* @throws ParseException
*/
public static calendar[] Getmonthstartandend (String monthstr) throws ParseException {
Calendar[] MSE = new CALENDAR[2];
Mse[0] = String2calendar (Monthstr, Month_pattern);
if (null = = Mse[0]) {
Mse[0] = Calendar.getinstance ();
Mse[0].set (calendar.date, 1);
}
MSE[1] = (Calendar) mse[0].clone ();
Mse[1].set (Calendar.month, Mse[1].get (calendar.month) + 1);
Mse[1].set (Calendar.date, Mse[1].get (calendar.date)-1);
Mse[1].set (Calendar.hour_of_day, 23);
Mse[1].set (Calendar.minute, 59);
Mse[1].set (Calendar.second, 59);
Mse[1].set (Calendar.millisecond, 998);
return MSE;
}

/**
* Get the first day of the previous one months
*
* @param c
* @return
*/
public static calendar startoflastmonth (Calendar c) {
C.add (Calendar.month,-1);
C.set (Calendar.day_of_month, 1);
return Startofday (c);
}

/**
* Get the last day of the previous one months
*
* @param c
* @return
*/
public static calendar endoflastmonth (Calendar c) {
C.set (calendar.day_of_month, 0);
return Endofday (c);
}

/**
* Started last week (Sunday is a week)
*
* @param c
* @return
*/
public static calendar Startoflastweek (Calendar c) {
C.add (Calendar.week_of_year,-1);
C.set (Calendar.day_of_week, calendar.sunday);
return Startofday (c);
}

/**
* End of last week (ended in Saturday)
*
* @param c
* @return
*/
public static calendar Endoflastweek (Calendar c) {
C.add (Calendar.week_of_year,-1);
C.set (Calendar.day_of_week, calendar.saturday);
return Endofday (c);
}

/**
* Starting this week (Sunday for a week)
*
* @param c
* @return
*/
public static calendar Startofweek (Calendar c) {
C.set (Calendar.day_of_week, calendar.sunday);
return Startofday (c);
}

/**
* End of the week
* Saturday is the end of the week
*
* @param c
* @return
*/
public static calendar Endofweek (Calendar c) {
C.set (Calendar.day_of_week, calendar.saturday);
return Endofday (c);
}

/**
* Gets the number of days in the month
* */
public static int Getcurrentmonthlastday () {
Calendar a = Calendar.getinstance ();
A.set (calendar.date, 1);//Set the date to the first day of the month
A.roll (Calendar.date,-1);//Date Roll back one day, that is the last day
int maxdate = A.get (calendar.date);
return maxdate;
}

/**
* First day of the month
* */

public static calendar startofmonth (Calendar cal) {
Cal.set (Calendar.day_of_month, 1);
Return Startofday (CAL);
}

/**
* Last day of the month
* @param c
* @return
*/
public static calendar endOfMonth (Calendar c) {
C.set (Calendar.date, C.getactualmaximum (Calendar.day_of_month));
return Endofday (c);
}

Gets the number of weeks of the current time in the year
public static int Getweekofyear (date date) {
Calendar C = new GregorianCalendar ();
C.setfirstdayofweek (Calendar.monday);
C.setminimaldaysinfirstweek (7);
C.settime (date);
Return C.get (calendar.week_of_year);
}

/********************************************************************** Split Line ************************************* ******************/

Usually the finance department will have one day of the year as the fiscal year to start to the second year of the day

Take September 26 as an example

/**
* Get the deviation value
* Get the number of milliseconds from September 26 to the end of the year
* @return
*/
public static long Getdeviationtimeinmillis () {
Calendar Nowcalendar = Calendar.getinstance ();
Nowcalendar.set (Calendar.month, 8);
Nowcalendar.set (Calendar.day_of_month, 26);
Calendar startTime = Startofday (Nowcalendar);

Calendar instance = Calendar.getinstance ();
Calendar year = Startofyear ((Instance.get (calendar.year) + 1));

Long time = Year.gettimeinmillis ()-Starttime.gettimeinmillis ();

return time;
}

/**
* Get relative time
* Get the number of milliseconds from September 26 to the end of the year

* Gets the current time of the number of milliseconds + deviation in milliseconds
* @return
*/

public static calendar getdeviationtime (Calendar c) {
Long Timeinmillis = C.gettimeinmillis ();
Long deviationmills = Timeinmillis + Getdeviationtimeinmillis ();
Calendar time = Calendar.getinstance ();
Time.settimeinmillis (Deviationmills);
System.err.println ("yyyyyyy++" +calendar2string (time));
System.err.println ("dayofyear3++" + (Time.get (calendar.day_of_year)));
return time;
}

/**
* Get relative Week number

* @return
*/

public static Integer Getweekofyear (Calendar nowtime) throws ParseException {
= Calendar.getinstance ();
Nowtime.add (Calendar.month, 8);
System.err.println ("--nowtime--" +
Timetools.calendar2string (Nowtime));
int nowmonth = Nowtime.get (calendar.month) + 1;
int year = Nowtime.get (calendar.year);
Int week = 0;
if (Nowmonth > 9) {
String standardtimestr = year + "-09-26 00:00:00";
Calendar standardtime = Timetools.string2calendar (STANDARDTIMESTR);
if (standardtime = = null) {
SYSTEM.ERR.PRINTLN ("-----------Error Error Error-------------");
return null;
}
int nowweekofyear = Nowtime.get (calendar.week_of_year);
int standardweekofyear = Standardtime.get (calendar.week_of_year);
Week = Nowweekofyear-standardweekofyear;
System.out.println ("nowweekofyear++" +nowweekofyear+ "===standardweekofyear++" +standardweekofyear+ "===++" +week );
} else {
The week after September 26 last year is the first week
Number of weeks after 26 last September + current week.
String standardtimestr = (year-1) + "-09-26 00:00:00";
Calendar standardtime = Timetools.string2calendar (STANDARDTIMESTR);
if (standardtime = = null) {
SYSTEM.ERR.PRINTLN ("-----------Error Error Error-------------");
return null;
}
Weeks after September 26 of last year
int standardweekofyear = Standardtime.get (calendar.week_of_year) + 1;
Get total weeks of last year
System.err.println ((year-1) + "--------------+ +" +sumweek (year-1));
int sumweek = Sumweek (year-1);
Calculate the remaining weeks of the year
int otherweek = Sumweek-standardweekofyear;
Gets the current number of weeks
int nowweek = Nowtime.get (calendar.week_of_year);
Week = Nowweek + Otherweek;
}

Return week;
}

Given the year, and went to the relative beginning of the years

public static Calendar startofdeviationyear (int year, int month) {
Calendar Nowcalendar = Calendar.getinstance ();
if (month >= 8) {
Nowcalendar.set (Calendar.year, year);
} else {
Nowcalendar.set (Calendar.year, year-1);
}
Nowcalendar.set (Calendar.month, 8);
Nowcalendar.set (Calendar.day_of_month, 26);
Return Startofday (Nowcalendar);
}

Given the year, and went to the relative end of the years

public static Calendar endofdeviationyear (int year, int month) {

Calendar Nowcalendar = Calendar.getinstance ();
if (month >= 8)
Nowcalendar.set (calendar.year, year + 1);
Else
Nowcalendar.set (Calendar.year, year);

Nowcalendar.set (Calendar.month, 8);
Nowcalendar.set (Calendar.day_of_month, 25);
Return Endofday (Nowcalendar);
}

Relative early month of the given time

public static calendar startofdeviationmonth (Calendar c) {
if (C.get (Calendar.day_of_month) < 26)
C.add (Calendar.month,-1);
C.set (Calendar.day_of_month, 26);
return Startofday (c);
}

Given time relative to month end

public static calendar endofdeviationmonth (Calendar c) {
int dayofmonth = C.get (calendar.day_of_month);
if (DayOfMonth > 26)
C.add (Calendar.month, 1);
C.set (Calendar.day_of_month, 25);
return Endofday (c);
}

Get the number of days in the current time corresponding month

public static int Getdeviationmonthsumday () {
Calendar instance = Calendar.getinstance ();
Calendar C = getdeviationtime (instance);
return Getcurrentmonthsumday (c);
}

Get the number of days in the month for a given time

private static int getcurrentmonthsumday (Calendar c) {
C.set (calendar.date, 1);//Set the date to the first day of the month
C.roll (Calendar.date,-1);//Date Roll back one day, that is the last day
Return C.get (calendar.date);
}

/**
* Starting this week
* Starting from Monday
* @param c
* @return
*/
public static calendar startofweekwithmonday (Calendar c) {
C.set (Calendar.day_of_week, calendar.monday);
return Startofday (c);
}

/**
* End of the week
* End of Sunday
* @param c
* @return
*/
public static calendar endofweekwithsunday (Calendar c) {
C.add (calendar.week_of_year, 1);
C.set (Calendar.day_of_week, calendar.sunday);
return Endofday (c);
}

/**
* Start next week
* Starting from Monday
* @param c
* @return
*/

public static calendar startofnextweekwithmonday (Calendar c) {
C.add (calendar.week_of_year, 1);
C.set (Calendar.day_of_week, calendar.monday);
return Startofday (c);
}

/**
* Started last week
* Starting from Monday
* @param c
* @return
*/
public static calendar startoflastweekwithmonday (Calendar c) {
C.add (Calendar.week_of_year,-1);
C.set (Calendar.day_of_week, calendar.monday);
return Startofday (c);
}

/**
* End of last week
* End of Sunday
* @param c
* @return
*/
public static calendar endoflastweekwithsunday (Calendar c) {
C.add (Calendar.week_of_year,-1);
C.set (Calendar.day_of_week, calendar.sunday);
return Endofday (c);
}

Java Time processing class

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.