Java various date calculations

Source: Internet
Author: User

1, the time format of the general processing.

/**
* Formats the user given time in the specified time format. <br/>
*
* @param dateTime Time
* @param datetimefmat time format
* @return String-The time content after formatting
*/
public static final String fmatlongtostring (long dateTime, string datetimefmat) {
return new SimpleDateFormat (Datetimefmat). Format (dateTime);
}
/**
* String time is converted to long type processing. <br/>
*
* @param Date Time string
* @param fmat time format
* @return Long-the result of the conversion (return -1l on exception)
*/
public static final long Fmatstringtolong (string date, string Fmat) {
Long rsltdate = -1l;
try {
Rsltdate = new SimpleDateFormat (Fmat). Parse (date). GetTime ();
} catch (ParseException e) {
Rsltdate = -1l;
}
return rsltdate;
}

2. The week of 1st and the Sunday day of the week are obtained on the specified date.

/**
* The week of 1st of the week on which the specified date is obtained. <br/>
*
* @param datetime specified date
* @return Long-Date Week of 1st
*/
public static final long getfrstdateofweekbydate (long dateTime) {

Date-Processing Object initialization
Calendar Cldr = Calendar.getinstance ();
Specify Time settings
Cldr.settimeinmillis (DateTime);

Specify the time period to judge
if (Cldr.get (calendar.day_of_week) = = Calendar.sunday) {

Sunday, calculated this week 1st
Cldr.add (Calendar.day_of_month,-6);
} else if (Cldr.get (Calendar.day_of_week) > Calendar.monday) {

From Tuesday to Saturday, the 1st period of this week is calculated
Cldr.add (Calendar.day_of_month, (2-cldr.get (Calendar.day_of_week)));
}

Processing results returned
return Cldr.gettimeinmillis ();
}
/**
* The Sunday Day of the week on which the specified date is made. <br/>
*
* @param datetime specified date
* @return Long-the day of the week on which the date is located
*/
public static final long getlastdateofweekbydate (long dateTime) {

Date-Processing Object initialization
Calendar Cldr = Calendar.getinstance ();
Specify Time settings
Cldr.settimeinmillis (DateTime);

Specify the time period to judge
if (Cldr.get (calendar.day_of_week)! = Calendar.sunday) {

From Monday to Saturday, the day of the week is calculated
Cldr.add (Calendar.day_of_month, (8-cldr.get (Calendar.day_of_week)));
}

Processing results returned
return Cldr.gettimeinmillis ();
}

Java various date calculations

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.