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