Java gets time this week, this month, the beginning of the quarter.

Source: Internet
Author: User

Package com.yong.util;
Import Java.util.Calendar;
Import Java.util.Date;

public class TestDate {

public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println ("Current time:" + new Date (). toLocaleString ());
System.out.println ("0 Hours of the day:" + gettimesmorning (). toLocaleString ());
System.out.println ("24 Hours of the day:" + gettimesnight (). toLocaleString ());
SYSTEM.OUT.PRINTLN ("This week Monday 0 o'clock Time:" + gettimesweekmorning (). toLocaleString ());
SYSTEM.OUT.PRINTLN ("This week Sunday 24 o'clock Time:" + gettimesweeknight (). toLocaleString ());
System.out.println ("At the beginning of this month 0 hours:" + gettimesmonthmorning (). toLocaleString ());
System.out.println ("Not 24 points this month:" + gettimesmonthnight (). toLocaleString ());
}

Get 0 points on the day
public static Date gettimesmorning () {
Calendar cal = Calendar.getinstance ();
Cal.set (calendar.hour_of_day, 0);
Cal.set (Calendar.second, 0);
Cal.set (calendar.minute, 0);
Cal.set (Calendar.millisecond, 0);
return Cal.gettime ();
}

Get 24 points on the day
public static Date Gettimesnight () {
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar.hour_of_day, 24);
Cal.set (Calendar.second, 0);
Cal.set (calendar.minute, 0);
Cal.set (Calendar.millisecond, 0);
return Cal.gettime ();
}

Get this Monday 0 o'clock time
public static Date gettimesweekmorning () {
Calendar cal = Calendar.getinstance ();
Cal.set (Cal.get (calendar.year), Cal.get (Calendar.monday), Cal.get (Calendar.day_of_month), 0, 0, 0);
Cal.set (Calendar.day_of_week, calendar.monday);
return Cal.gettime ();
}

Get this Sunday 24 o'clock time
public static Date Gettimesweeknight () {
Calendar cal = Calendar.getinstance ();
Cal.settime (Gettimesweekmorning ());
Cal.add (Calendar.day_of_week, 7);
return Cal.gettime ();
}

Get the first day of the month 0 o'clock time
public static Date gettimesmonthmorning () {
Calendar cal = Calendar.getinstance ();
Cal.set (Cal.get (calendar.year), Cal.get (Calendar.monday), Cal.get (Calendar.day_of_month), 0, 0, 0);
Cal.set (Calendar.day_of_month, Cal.getactualminimum (Calendar.day_of_month));
return Cal.gettime ();
}

Get 24 minutes on the last day of the month.
public static Date Gettimesmonthnight () {
Calendar cal = Calendar.getinstance ();
Cal.set (Cal.get (calendar.year), Cal.get (Calendar.monday), Cal.get (Calendar.day_of_month), 0, 0, 0);
Cal.set (Calendar.day_of_month, Cal.getactualmaximum (Calendar.day_of_month));
Cal.set (Calendar.hour_of_day, 24);
return Cal.gettime ();
}

}

/** 
* Start time of the current quarter
* 
* @return  
*/ 
Public Date getcurrentquarterstarttime () {
Calendar C = calendar.getinstance ();
int currentmonth = C.get (calendar.month) + 1;
Date now = null;
try {
if (currentmonth >= 1 && currentmonth <= 3)
C.set (Calendar.month, 1);
Else if (currentmonth >= 4 && currentmonth <= 6)
C.set (Calendar.month, 3);
Else if (currentmonth >= 7 && currentmonth <= 9)
C.set (Calendar.month, 4);
Else if (currentmonth >= && currentmonth <=)
C.set (Calendar.month, 9);
C.set (calendar.date, 1);
now = Longsdf.parse (Shortsdf.format (C.gettime ()) + "00:00:00");
} catch (Exception e) {
E.printstacktrace ();
}
return now;
}

/**
* End time of the current quarter
*
* @return
*/
Public Date Getcurrentquarterendtime () {
Calendar C = calendar.getinstance ();
int currentmonth = C.get (calendar.month) + 1;
Date now = null;
try {
if (currentmonth >= 1 && currentmonth <= 3) {
C.set (Calendar.month, 2);
C.set (Calendar.date, 31);
} else if (Currentmonth >= 4 && currentmonth <= 6) {
C.set (Calendar.month, 5);
C.set (Calendar.date, 30);
} else if (currentmonth >= 7 && currentmonth <= 9) {
C.set (Calendar.month,;
C.set (Calendar.date, 30);
} else if (currentmonth >= && currentmonth <= 12) {
C.set (Calendar.month, 11);
C.set (Calendar.date, 31);
}
now = Longsdf.parse (Shortsdf.format (C.gettime ()) + "23:59:59");
} catch (Exception e) {
E.printstacktrace ();
}
return now;
}

Java gets time this week, this month, the beginning of the quarter.

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.