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