Java.util (date and calendar)

Source: Internet
Author: User

public class date implements Java.io.Serializable, cloneable, comparable<date>{public Date () {//Current time     This (System.currenttimemillis ());        The public date (long date) {//assigns the Date object and initializes the object to represent the specified number of milliseconds since the standard base time, known as the epoch, or January 1, 1970 00:00:00 GMT.    Fasttime = date;    } @Deprecated public Date (int, int, month, int date) {This (year, month, Date, 0, 0, 0); } @Deprecated public Date (int, int month, int date, int hrs, int min) {This (year, month, Date, hrs, min, 0)    ;        } ... public String toString () {//"EEE MMM dd HH:mm:ss zzz yyyy";...... "."        Basecalendar.date Date = normalize ();        StringBuilder sb = new StringBuilder (28);        int index = Date.getdayofweek (); if (index = = gcal.        SUNDAY) {index = 8;                        } converttoabbr (SB, Wtb[index]). Append (");  EEE converttoabbr (SB, Wtb[date.getmonth ()-1 + 2 + 7]). Append (");       MMM CALENDARUTILS.SPRINTF0D (SB, Date.getdayofmonth (), 2). Append (");   DD calendarutils.sprintf0d (SB, Date.gethours (), 2). Append (': '); HH calendarutils.sprintf0d (SB, Date.getminutes (), 2). Append (': '); MM calendarutils.sprintf0d (SB, Date.getseconds (), 2). Append (");        SS TimeZone Zi = Date.getzone (); if (Zi! = null) {Sb.append (Zi.getdisplayname (Date.isdaylighttime (), Zi. Short, locale.us));        ZZZ} else {sb.append ("GMT");  } sb.append ("). Append (Date.getyear ());    yyyy return sb.tostring (); }}

The Date represents the number of milliseconds 00:00:00 from 1970-01-01, and the value is independent of the time zone. Most of the date methods are obsolete and are replaced by the method of the Calendar class

public class Datetest {@SuppressWarnings ("deprecation") public static void main (string[] args) {Date d = new Date (); System.out.println (d); System.out.println (System.currenttimemillis ()); Long L = Date.parse ("Sun Sep 03:16:48 CST 2017"); System.out.println (L);D ate data = new Date (l); SYSTEM.OUT.PRINTLN (data);D ate day1=new Date (11,3,4); System.out.println (Day1);}} Output: Sun Sep 03:17:22 CST 201715049846420471505035008000Sun Sep 17:16:48 CST 2017Tue Apr 00:00:00 CST 1911
Calendar
Public abstract class Calendar implements Serializable, Cloneable, comparable<calendar> {protected calendar ()        {This (Timezone.getdefaultref (), Locale.getdefault (Locale.Category.FORMAT));    Sharedzone = true;        } protected Calendar (TimeZone zone, Locale alocale) {fields = new Int[field_count];        IsSet = new Boolean[field_count];        Stamp = new Int[field_count];        This.zone = Zone;    Setweekcountdata (Alocale); } public static Calendar getinstance () {Calendar cal = Createcalendar (Timezone.getdefaultref (), L        Ocale.getdefault (Locale.Category.FORMAT));        Cal.sharedzone = true;    Return cal; } public static Calendar getinstance (TimeZone zone) {return Createcalendar (Zone, Locale.getdefault (Lo Cale.    Category.format));    Public final Date GetTime () {return new date (Gettimeinmillis ()); The public final void settime (date date) {Settimeinmillis (date.gettime());        } public Long Gettimeinmillis () {if (!istimeset) {updatetime ();    } return time; }........

The Calendar class is an abstract class, and the static method of the Calendar class getinstance () can initialize a Calendar object: calendar now = Calendar.getinstance ();

You can use the following three methods to set the calendar to any one time:
  Set (int year, int month,int date)  
  
  
If you want to get the year, month, hour and other information you can use:  
Now.get (calendar.month); This method 0 means January, 1 means February  
  Get (Calendar.day_of_month) get the day of the month
  
  Get (Calendar.day_of_year) get the day of the year  
  Gettimemillis () Gets the millisecond representation of the current time
Conversion of calendar and date
(1) Calendar converted 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);
Formatted output date Time:

    Date data = new Date ();
The SimpleDateFormat class is used to parse and format the date string output
SimpleDateFormat f2=new SimpleDateFormat ("YYYY year mm month DD Day hh mm min ss seconds");
SimpleDateFormat f3=new SimpleDateFormat ("Yy/mm/dd hh:mm");
SimpleDateFormat f4=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//equivalent to now.tolocalestring ()
SimpleDateFormat f5=new SimpleDateFormat ("yyyy years mm DD Day hh mm min ss sec E");
System.out.println (F2.format (d));
System.out.println (F3.format (d));
System.out.println (F4.format (d));
System.out.println (F5.format (d));

About the implementation class for the calendar: GregorianCalendar

JavaOnly carry the Gregorian calendar (Gregorian, solar) implementation, that is the java.util.GregorianCalendar class

  Calendar calendar = newGregorianCalendar();

  Calendar类有着对应的get方法,让你获取到年、月、日、小时、分钟、秒、毫秒和给定日期的时区

  CalendarClass has a corresponding set method, so you can also set these fields

Java.util (date and calendar)

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.