Java Get day, week, month start and end time

Source: Internet
Author: User

1. Code
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 StubSystem.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 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 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 ();}}


2. Results
Current time: 2014-10-17 15:23:26 Day 0 Time: 2014-10-17 0:00:00 Day 24 point time: 2014-10-18 0:00:00 Monday 0 o'clock time: 2014-10-13 0:00:00 this week Sunday 24 o'clock time: 2014-10-20 0:00:00 early this month 0 point time: 2014-10-1 0:00:00 this month not 24 time: 2014-11-1 0:00:00



Java Get day, week, month start and end time

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.