Date D=new date ();
SimpleDateFormat df=new SimpleDateFormat ("Yyyy-mm-dd");
System.out.println ("Today's Date:" +df.format (d));
System.out.println ("Two days ago Date:" + Df.format (new Date (D.gettime ()-(LONG) 2 * 24 * 60 * 60 * 1000)));
System.out.println ("Date after three days:" + df.format (new Date (D.gettime () + (LONG) 3 * 24 * 60 * 60 * 1000)));
Java processing Date Time common methods:
1.java.util.calendar
The Calendar class is an abstract class that provides methods for converting between specific moments and a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and provides methods for manipulating calendar fields, such as getting the date of the next week. The instantaneous value is expressed as a millisecond, which is the offset from the calendar element (that is, Greenwich Mean Time January 1, 1970, 00:00:00.000, Gregorian calendar).
Cases:
Java code
Calendar cal = Calendar.getinstance ();//Use the default time zone and locale to get a calendar.
Cal.add (Calendar.day_of_month,-1);//Take the day before the current date.
Cal.add (Calendar.day_of_month, + 1);//Take the day after the current date.
Format output Date by
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
System.out.println ("Today is:" +format.format (Calendar.getinstance (). GetTime ()));
System.out.println ("Yesterday is:" +format.format (Cal.gettime ()));
Get 2007-12-25 Date:
Java code
Calendar calendar = new GregorianCalendar (2007, 11, 25,0,0,0);
Date date = Calendar.gettime ();
System.out.println ("Christmas is:" +format.format (date));
The Java month is from 0-11, and the month is set to minus 1.
GregorianCalendar constructor method parameters are: year, month-1, day, time, minute, second.
Take the date part:
Java code
int year =calendar.get (calendar.year);
int Month=calendar.get (calendar.month) +1;
int day =calendar.get (calendar.day_of_month);
int hour =calendar.get (calendar.hour_of_day);
int minute =calendar.get (calendar.minute);
int seconds =calendar.get (calendar.second);
Add 1 to the month you take.
Determine the maximum number of days in the current month:
Java code
Calendar cal = Calendar.getinstance ();
int Day=cal.getactualmaximum (calendar.day_of_month);
System.out.println (day);
2.java.util.date
Java code
Java.util.Date today=new java.util.Date ();
System.out.println ("Today is" +formats.format (today));
Take the first day of the month:
Java code
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("yyyy-mm-01");
Java.util.Date firstday=new java.util.Date ();
System.out.println ("The Month first day is" +formats.format (FirstDay));
Take the last day of the month:
Java code
Calendar cal = Calendar.getinstance ();
int Maxday=cals.getactualmaximum (calendar.day_of_month);
Java.text.Format formatter3=new Java.text.SimpleDateFormat ("yyyy-mm-" +maxday);
System.out.println (Formatter3.format (Cal.gettime ()));
Find the number of days between two dates:
Java code
Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date begindate= format.parse ("2007-12-24");
Java.util.Date enddate= format.parse ("2007-12-25");
Long day= (Date.gettime ()-mydate.gettime ())/(24*60*60*1000);
System.out.println ("number of days apart =" +day);
Date of a year ago:
Java code
Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date todaydate=new java.util.Date ();
Long Beforetime= (Todaydate.gettime ()/1000) -60*60*24*365;
Todaydate.settime (beforetime*1000);
String Beforedate=formatter.format (todaydate);
System.out.println (beforedate);
Date of one year:
Java code
Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date todaydate=new java.util.Date ();
Long Aftertime= (Todaydate.gettime ()/1000) +60*60*24*365;
Todaydate.settime (aftertime*1000);
String Afterdate=formatter.format (todaydate);
System.out.println (afterdate);
Ask for 10 hours after the time
Java code
Java.util.Calendar cal=java.util.calendar.getinstance ();
Cal.settime (Dateoper);
Cal.add (java.util.calendar.hour_of_day,10);
System.out.println ("Date:" +forma.format (Cal.gettime ()));
10 hours before the time
Java code
Java.util.Calendar cal=java.util.calendar.getinstance ();
Cal.settime (Dateoper);
Cal.add (JAVA.UTIL.CALENDAR.HOUR_OF_DAY,-10);
System.out.println ("Date:" +forma.format (Cal.gettime ()));
3.java.sql.date
Inherit from Java.util.Date, which is the date type used to manipulate the database
Java code
Java.sql.Date sqldate = new Java.sql.Date (java.sql.Date.valueOf ("2007-12-25"). GetTime ());
Date Comparison: A simple comparison can be directly compared as a string, or you can use the
Java.sql.Date.valueOf ("2007-03-08"). CompareTo (Java.sql.Date.valueOf ("2007-03-18")) Way to compare the size of the date. You can also use Java.util.Date.after (java.util.Date) to compare.
Time difference:
Long Difference=c2.gettimeinmillis ()-c1.gettimeinmillis ();
Days difference: Long day=difference/(3600*24*1000)
Difference Hour: Long hour=difference/(3600*1000)
Minute difference: Long minute=difference/(60*1000)
Difference seconds: Long second=difference/1000
Add:
Java code
DateFormat df=new SimpleDateFormat ("Yyyy-mm-dd EE hh:mm:ss");
System.out.println (Df.format (New Date ()));
Date date = new Date ();
DateFormat shortdate=dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short);
DateFormat mediumdate =dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium);
DateFormat longdate =dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long);
DateFormat fulldate =dateformat.getdatetimeinstance (Dateformat.full, dateformat.full);
System.out.println (Shortdate.format (date));
System.out.println (Mediumdate.format (date));
System.out.println (Longdate.format (date));
System.out.println (Fulldate.format (date));
08-4-15 3:24
2008-4-15 15:24:31
April 15, 2008 03:24 P.M. 31 seconds
April 15, 2008 Tuesday 03:24 P.M. 31 sec CST
Calendar C = calendar.getinstance ();
C.add (Calendar.month, 1); The current time plus 1 months
System.out.println (Df.format (C.gettime ()));
C.add (Calendar.hour, 3); The current time plus 3 hours
System.out.println (Df.format (C.gettime ()));
C.add (Calendar.year,-2); It's now minus 2 years.
System.out.println (Df.format (C.gettime ()));
C.add (Calendar.day_of_week, 7); The current time plus 7 days
System.out.println (Df.format (C.gettime ()));
Java adds and subtract dates from date class, year plus minus, month plus minus