Java gets the start and end times of the day, this week, this month, the quarter, the year, etc.

Source: Internet
Author: User

1. Code
Package Com.zhaochao.utils;import Java.text.simpledateformat;import Java.util.calendar;import java.util.Date; public class Datauill {public static void main (string[] args) {//TODO auto-generated method StubSystem.out.println ("24 Point of the day Time: "+ gettimesnight (). toLocaleString ()); System.out.println ("Current time:" + new Date (). toLocaleString ()); System.out.println ("0 Hours of the day:" + gettimesmorning (). toLocaleString ()); SYSTEM.OUT.PRINTLN ("Yesterday 0 Hours:" + getyesterdaymorning (). toLocaleString ()); SYSTEM.OUT.PRINTLN ("Nearly 7 days time:" + Getweekfromnow (). 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 ()); System.out.println ("0 hours at the beginning of the month:" + getlastmonthstartmorning (). toLocaleString ()); System.out.println ("Time to start this quarter:" + getcurrentquarterstarttime (). toLocaleString ()); System.out.println ("The quarter end point time: "+ getcurrentquarterendtime (). toLocaleString ()); System.out.println ("Starting point of the year:" + Getcurrentyearstarttime (). toLocaleString ()); SYSTEM.OUT.PRINTLN ("Year end point time:" + getcurrentyearendtime (). toLocaleString ()); System.out.println ("Last year start point time:" + getlastyearstarttime (). toLocaleString ());} Get the day 0 point time 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 yesterday 0 point time public static Date getyesterdaymorning () {Calendar cal = Calendar.getinstance (); Cal.settimeinmillis ( Gettimesmorning (). GetTime () -3600*24*1000); return Cal.gettime ();} Get the same day nearly 7 days public static date Getweekfromnow () {Calendar cal = Calendar.getinstance (); Cal.settimeinmillis ( Gettimesmorning (). GetTime () -3600*24*1000*7); return Cal.gettime ();} Get the day 24 point time 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 the last day of the month 24 point time 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); return Cal.gettime ();} public static Date getlastmonthstartmorning () {Calendar cal = Calendar.getinstance (); Cal.settime (gettimesmonthmorning ()); Cal.add (Calendar.month,-1); return Cal.gettime ();} public static Date Getcurrentquarterstarttime () {Calendar c = calendar.getinstance (); int currentmonth = C.get ( Calendar.month) + 1; SimpleDateFormat longsdf = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); SimpleDateFormat shortsdf = new SimpleDateFormat ("YYYY-MM-DD");D ate now = null;try {if (currentmonth >= 1 && cu Rrentmonth <= 3) c.set (calendar.month, 0); 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;} /** * The end time of the current quarter. That is 2012-03-31 23:59:59 * * @return */public static Date getcurrentquarterendtime () {Calendar cal = Calendar.getinstance (); CA L.settime (Getcurrentquarterstarttime ()); Cal.add (Calendar.month, 3); return Cal.gettime ();} public static Date Getcurrentyearstarttime () {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.year)); return Cal.gettime ();} public static Date Getcurrentyearendtime () {Calendar cal = Calendar.getinstance (); Cal.settime (getcurrentyearstarttime ()); Cal.add (calendar.year, 1); return Cal.gettime ();} public static Date Getlastyearstarttime () {Calendar cal = Calendar.getinstance (); Cal.settime (Getcurrentyearstarttime ( ); Cal.add (Calendar.year,-1); return Cal.gettime ();}}


2. Results
Day 24 time: 2015-1-18-0:00:00 Current time: 2015-1-17 10:50:57 Day 0 point time: 2015-1-17 0:00:00 yesterday 0 point time: 2015-1-16 0:00:00 nearly 7 days time: 2015-1-10 0:00:00 this week Monday 0 o'clock time: 2015-1-12 0:00:00 this week Sunday 24 o'clock time: 2015-1-19 0:00:00 early this month 0 point time: 2015-1-1 0:00:00 this month not 24 point time: 2015-2-1 0:00:00 early 0:2014-12-1 0:00:00 the beginning of the season: 2015-1-1 0:00:00 this quarter end point time: 2015-4-1 0:00:00 this year start point time: 2015-1-1 0:00:00 year end point time: 2016-1-1 0:00:00 last year start point time: 2014-1-1 0:00:00



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Java gets the start and end times of the day, this week, this month, the quarter, the year, etc.

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.