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).
Example:
Java code
1. Calendar cal = Calendar.getinstance ();//Use the default time zone and locale to get a calendar.
2. Cal.add (Calendar.day_of_month,-1);//Take the day before the current date.
3.
4. Cal.add (Calendar.day_of_month, + 1);//Take the day after the current date.
5.
6.//Format output date
7. Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
8.
9. 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
1. Calendar calendar = new GregorianCalendar (2007, 11, 25,0,0,0);
2. Date date = Calendar.gettime ();
3. 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
1. int year =calendar.get (calendar.year);
2.
3. int Month=calendar.get (calendar.month) +1;
4.
5. Int day =calendar.get (calendar.day_of_month);
6.
7. Int hour =calendar.get (calendar.hour_of_day);
8.
9. int minute =calendar.get (calendar.minute);
.
One . 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
1. Calendar cal = Calendar.getinstance ();
2. Int day=cal.getactualmaximum (calendar.day_of_month);
3. SYSTEM.OUT.PRINTLN (day);
2.java.util.date
Java code
1. Java.util.Date today=new java.util.Date ();
2. System.out.println ("Today is" +formats.format (today));
take the first day of the month:
Java code
1. Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("yyyy-mm-01");
2. Java.util.Date firstday=new java.util.Date ();
3. System.out.println ("The Month first day is" +formats.format (FirstDay));
take the last day of the month:
Java code
1.
2. Calendar cal = Calendar.getinstance ();
3. int maxday=cals.getactualmaximum (calendar.day_of_month);
4. Java.text.Format formatter3=new Java.text.SimpleDateFormat ("yyyy-mm-" +maxday);
5. System.out.println (Formatter3.format (Cal.gettime ()));
find the number of days between two dates:
Java code
1. Java.text.SimpleDateFormat format = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
2. Java.util.Date begindate= format.parse ("2007-12-24");
3. Java.util.Date enddate= format.parse ("2007-12-25");
4. Long day= (Date.gettime ()-mydate.gettime ())/(24*60*60*1000);
5. System.out.println ("number of days apart =" +day);
date of a year ago:
Java code
1. Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
2. Java.util.Date todaydate=new java.util.Date ();
3. Long Beforetime= (Todaydate.gettime ()/1000) -60*60*24*365;
4. Todaydate.settime (beforetime*1000);
5. String Beforedate=formatter.format (todaydate);
6. System.out.println (beforedate);
date of one year:
Java code
1. Java.text.Format formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
2. Java.util.Date todaydate=new java.util.Date ();
3. Long Aftertime= (Todaydate.gettime ()/1000) +60*60*24*365;
4. Todaydate.settime (aftertime*1000);
5. String Afterdate=formatter.format (todaydate);
6. System.out.println (afterdate);
ask for 10 hours after the time
Java code
1. Java.util.Calendar cal=java.util.calendar.getinstance ();
2. Cal.settime (Dateoper);
3. Cal.add (java.util.calendar.hour_of_day,10);
4. System.out.println ("Date:" +forma.format (Cal.gettime ()));
10 hours before the time
Java code
1. Java.util.Calendar cal=java.util.calendar.getinstance ();
2. Cal.settime (Dateoper);
3. Cal.add (JAVA.UTIL.CALENDAR.HOUR_OF_DAY,-10);
4. 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
1. 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
1. DateFormat df=new SimpleDateFormat ("Yyyy-mm-dd EE hh:mm:ss");
2. System.out.println (Df.format (New Date ()));
3. Date date = new Date ();
4. DateFormat shortdate=dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short);
5. DateFormat mediumdate =dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium);
6. DateFormat longdate =dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long);
7. DateFormat fulldate =dateformat.getdatetimeinstance (Dateformat.full, dateformat.full);
8.
9. System.out.println (Shortdate.format (date));
System.out.println (Mediumdate.format (date));
System.out.println (Longdate.format (date));
System.out.println (Fulldate.format (date));
.
14.08-4-15 pm 3:24
2008-4-15 15:24:31
16. April 15, 2008 03:24 P.M. 31 seconds
17. April 15, 2008 Tuesday 03:24 P.M. 31 sec CST
.
.
Calendar C = calendar.getinstance ();
.
C.add (Calendar.month, 1);//Current time plus 1 month
System.out.println (Df.format (C.gettime ()));
A .
C.add (Calendar.hour, 3);//Current time plus 3 hours
System.out.println (Df.format (C.gettime ()));
.
C.add (Calendar.year,-2);//Current time minus 2 years
System.out.println (Df.format (C.gettime ()));
.
C.add (Calendar.day_of_week, 7);//Current time plus 7 days
System.out.println (Df.format (C.gettime ()));
Java processing Date Time common methods: