Found in the course of use,
public static void Main (String args[]) {
Date date = new Date ();
System.out.println (Date.getyear ();
}
The output is: 114 and the GetYear () method is no longer recommended.
So I thought of the calendar.
The correct way is:
public static void Main (String args[]) {
date date=new date ();
Calendar cal=calendar.getinstance ();
Cal.settime (date);
System.out.println (Cal.get (calendar.year));
}
The above is the conversion process.
Calendar turn to date:
(1) Calendar conversion to date ()
calendar cal=calendar.getinstance ();
Date () date=cal.gettime ();
(2) Date converted to Calendar
date date=new date ();
Calendar cal=calendar.getinstance ();
Cal.settime (date);
Resources:
What year is this?
MONTH This is the month (starting from 0)
Week_of_year the first few weeks of this year
Week_of_month the first weeks of the month (from the outset)
Date Today date
Day_of_month the first day of the month
Day_of_year the first day of the year
Day_of_week this week (starting from the day, calculated from 1)
Day_of_week_in_month Week (starting from the first day of the week)
AM_PM morning or afternoon 0 for the morning, 1 for the afternoon
HOUR 12 Hour system time
Hour_of_day 24 Hour system time
MINUTE How many points
SECOND How many seconds
Millisecond how many milliseconds
Zone_offset Time Zone
Dst_offset
Date D=new date ();
SimpleDateFormat df=new SimpleDateFormat ("Yyyy-mm-dd");
System.out.println ("Today's Date:" +df.format (d));
SYSTEM.OUT.PRINTLN ("Date Two days ago:" + Df.format (new Date (D.gettime ()-(LONG) 2 * 24 * 60 * 60 * 1000));
System.out.println ("Three days after Date:" + 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 a specific moment to transform between a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and for manipulating calendar fields, such as getting the date of next week. Instantly available in milliseconds, it is an offset from the 00:00:00.000 (i.e. Greenwich Mean time, January 1, 1970, 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.
To format the date of the output
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 ("2007 Christmas is:" +format.format (date));
The Java month is from 0-11, and the month is set to minus 1.
GregorianCalendar Construction method parameters are: year, month-1, day, hour, minute, second.
Take the part of the date:
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);
Take the month to add 1.
Determine the maximum number of days for 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);
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 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 ("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);
10 hours, please.
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 ()));
Please, 10 hours ago.
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
Inherits from Java.util.Date, is the date type that the operation database uses
Java code java.sql.Date sqldate = new Java.sql.Date (java.sql.Date.valueOf ("2007-12-25"). GetTime ());
Date Comparisons: 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")) method 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 of difference: Long day=difference/(3600*24*1000)
Difference hours: 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 (New Date ()) (Df.format);
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); Time plus 3 hours.
System.out.println (Df.format (C.gettime ()));
C.add (Calendar.year,-2); The current time 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 ()));