Java Date-related formatting (ii)

Source: Internet
Author: User

Import Java.util.Calendar;

Import Java.util.Date;
Import Java.util.GregorianCalendar;

public class Zcalendarutils {

/**
* Take last year's Today
* @return
*/
public static String Getlastyear () {
Calendar cal = Calendar.getinstance ();
Cal.add (Calendar.year,-1);
Date d = cal.gettime ();
Return Datetimeutils.formattodatetime (d);
}

/**
* Get a day of the week
* @param DT
* @return
*/
public static String getweekofdate (Date dt) {
String[] weekdays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getinstance ();
Cal.settime (DT);
int w = cal.get (Calendar.day_of_week)-1;
if (W < 0) {
w = 0;
}
return WEEKDAYS[W];
}
/**
* Take the current day of the week
* @return
*/
public static String Getcurrentweek () {
String day = getweekofdate (New Date ());
Return day;
}

/**
* Take the first day of the current month
* @return
*/
public static String Getfirstdayofcurrentmonth () {
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar.day_of_month, 1);
Date d = cal.gettime ();
Return Datetimeutils.formattodate (d);
}

/**
* Take yesterday
* @return
*/
public static String Getyesterday () {
Calendar cal = Calendar.getinstance ();
Cal.add (Calendar.day_of_month,-1);
Date d = cal.gettime ();
Return Datetimeutils.formattodate (d);
}

/**
* Take the last day of the current month
* @return
*/
public static String Getlastdayofcurrentmonth () {
Calendar cal = Calendar.getinstance ();
Cal.add (Calendar.month, 1);
Cal.set (Calendar.day_of_month, 1);
Cal.add (Calendar.day_of_month,-1);
Date d = cal.gettime ();
Return Datetimeutils.formattodate (d);
}
/**
* Take the first day of last month
* @return
*/
public static String Getfirstdayoflastmonth () {
Calendar cal = Calendar.getinstance ();
Cal.add (Cal. MONTH,-1);
Cal.set (Calendar.day_of_month, 1);
Date d = cal.gettime ();
Return Datetimeutils.formattodate (d);
}
/**
* Take last day of last month
* @return
*/
public static String Getlastdayoflastmonth () {
Calendar cal = Calendar.getinstance ();
Get the first day of the month on the system current time object
Cal.set (Calendar.day_of_month, 1);
Date minus one to get the last day of the month time object
Cal.add (Calendar.day_of_month,-1);
Date d = cal.gettime ();
Return Datetimeutils.formattodate (d);
}

/**
* Take the first few weeks of the current month
* @return
*/
public static int Getweeknumber () {
Calendar calendar = Calendar.getinstance ();
Calendar.setfirstdayofweek (Calendar.monday);
Calendar.settime (New Date ());
Return Calendar.get (Calendar.week_of_month)-1;
}
/**
* The week ordinal of the month in which the given date is taken
* @param newdate
* @param Week
* @return
*/
public static Calendar Getcalendar (Date newdate, int week) {
Calendar calenew = Calendar.getinstance ();
Calenew.settime (newdate);
Calenew.add (Calendar.week_of_month, week-1);
return calenew;
}
/**
* won the first day of last week
*
* @return
*/
public static String Getfirstdayoflastweek () {
int currentweekno = Zcalendarutils.getweeknumber ();
Date date = Zcalendarutils.getfirstdayofweek (new date (), currentWeekNo-1);
return datetimeutils.formattodate (date);
}
/**
* Get the first day of the current week
*
* @return
*/
public static String Getfirstdayofcurrentweek () {
int currentweekno = Zcalendarutils.getweeknumber ();
Date date = Zcalendarutils.getfirstdayofweek (new date (), currentweekno);
return datetimeutils.formattodate (date);
}
/**
* Get the last day of the week
*
* @return
*/
public static String Getlastdayoflastweek () {
int currentweekno = Zcalendarutils.getweeknumber ();
Date date = Zcalendarutils.getlastdayofweek (new date (), currentWeekNo-1);
return datetimeutils.formattodate (date);
}
/**
* Get the last day of the current week
*
* @return
*/
public static String Getlastdayofcurrentweek () {
int currentweekno = Zcalendarutils.getweeknumber ();
Date date = Zcalendarutils.getlastdayofweek (new date (), currentweekno);
return datetimeutils.formattodate (date);
}

/**
* Gets the first day of the nth week of the month on which a given date is located
*
* @return
*/
private static date Getfirstdayofweek (date month, int week) {
Calendar Mycale = Calendar.getinstance ();
try{
GregorianCalendar GC = (GregorianCalendar) getcalendar (month, week);
Mycale.settime (Gc.gettime ());
Mycale.set (Calendar.date, Gc.get (calendar.date)-Gc.get (Calendar.day_of_week) + 2);
}catch (Exception e) {
E.printstacktrace ();
}
return Mycale.gettime ();
}

/**
* Gets the last day of the nth week of the month of the given date
*
* @return
*/
private static date Getlastdayofweek (date month, int week) {
Calendar Mycale = Calendar.getinstance ();
try{
GregorianCalendar GC = (GregorianCalendar) getcalendar (month, week);
Mycale.settime (Gc.gettime ());
Mycale.set (Calendar.date, Gc.get (calendar.date) + 8-gc.get (Calendar.day_of_week));
}catch (Exception e) {
E.printstacktrace ();
}
return Mycale.gettime ();
}


public static int getyearofdate (date date) {
if (date = = null) {
return 1990;
}
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Return Calendar.get (calendar.year);
}

public static int getmonthofdate (date date) {
if (date = = null) {
return 1;
}
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Return Calendar.get (Calendar.month) + 1;
}

public static int getdayofdate (date date) {
if (date = = null) {
return 1;
}
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Return Calendar.get (Calendar.day_of_month);
}

public static int Getcurrentmonth () {
Return Getmonthofdate (New Date ());
}

public static String Getcurrentmonthstr () {
return Getcurrentmonth () + "";
}

public static String getmonthofdatestr (date date) {
return Getmonthofdate (date) + "";
}

/**
* Get the maximum number of days for the month in which a date is
*
* @param date
* @return
*/
public static int Getmaxdaysofmonth (date date) {
Calendar calendar = Calendar.getinstance ();
Calendar.settime (date);
Return Calendar.getactualmaximum (Calendar.day_of_month);
}

}

Java Date-related formatting (ii)

Related Article

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.