Getting Started with Java--date action class

Source: Internet
Author: User
Tags dateformat

Getting Started with Java--date action class Date class

  

is a simpler operation class, using the construction method of the Java.util.Data class and outputting it to get a full Date. The construction method is: public Data ();

Java.util package needs to be imported

Package Sep26;import Java.util.date;public class DataDemo01 {public static void main (string[] Args) {date date=new Date (); System.out.println ("the Current date is:" +date);}}
The current date is: Mon SEP 2016 10:57:57 CST

Use the Calendar class to get Rich-format dates

Calendar class

The Calendar class is an abstract class that can rely on the polymorphism of the object to instantiate the parent class through subclasses, and the subclass of the Calendar class is the GregorianCalendar class.

Property:

Serial number Constant Type Describe
1 public static final int year Int Acquired year
2 public static final int MONTH Int Get the Month
3 public static final int Day_of_month Int Acquisition Date
4 public static final int Hour_of_day Int Get an hour, 24-hour system
5

public static final int MINUTE

Int Get points
6 public static final int SECOND Int Get seconds
7 public static final int Milliseond Int Get milliseconds

Method:

ordinal method type description
1 public static Calendar getinstance () normal instantiating objects by default time zone
2 public Beelean after (obeject When) Normal to determine whether a date is before a date
3 public Beelean before (obeject when) normal determine if a date is after a date
4 public int get (int field) normal Returns the value of a given calendar field
Package Sep26;import Java.util.calendar;import Java.util.gregoriancalendar;public class DateDemo02 {public static void Main (string[] Args) {Calendar calendar=null;calendar=new GregorianCalendar (); System.out.println ("year:" +calendar.get (calendar.year)); System.out.println ("month:" +calendar.get (calendar.month) +1);//month is calculated from 0, so +1system.out.println ("day:" +calendar.get ( calendar.day_of_month)); System.out.println ("time:" +calendar.get (calendar.hour_of_day)); System.out.println ("points:" +calendar.get (calendar.minute)); System.out.println ("seconds:" +calendar.get (calendar.second)); System.out.println ("ms:" +calendar.get (calendar.millisecond));}}
Year: 2016 Months: 81 days: 26 o'clock: 11 minutes: 20 seconds: 3 ms: 384

The time it takes to get the system in this way is more complicated, so Java provides other convenient system time classes to show the customary system time

DateFormat class

The format class is a class that is used to format data, and the DateFormat class is a subclass of It: the DateFormat class is also an abstract class, but the abstract class provides a static method that allows you to get an instance of this class Directly.

Package Sep26;import Java.util.*;import java.text.*;p ublic class DateDemo03 {public static void main (string[] Args) {Date Format df1=null;dateformat df2=null;df1=dateformat.getdateinstance (dateformat.year_field,new Locale ("zh", "CN")); Df2=dateformat.getdatetimeinstance (dateformat.year_field,dateformat.era_field,new Locale ("zh", "CN")); System.out.println ("date:" +df1.format (new Date ())); System.out.println ("date:" +df2.format (new Date ()));}}
date:2016 September 26 date:2016 September 26 11:37 A.M. 59 sec CST
SimpleDateFormat class (most commonly Used)

First define a full date conversion template, which is marked by a specific date in the Template:

Serial number Mark Describe
1 yyyy Years
2 Mm Month
3 Dd Day
4 HH Hours
5 Mm Minutes
6 Ss Seconds
7 Ss Milliseconds

Getting Started with Java--date action class

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.