(Java) Date Action Class

Date, Calendar in the java.util package, and SimpleDateFormat in the java.text package are used primarily in program dev...
1. Date class
2. Calendar classes
3. DateFormat class
4. SimpleDateFormat class
V. Acquire Full Date (Example)

Date, Calendar in the java.util package, and SimpleDateFormat in the java.text package are used primarily in program development.

Article Directory

1. Date class

The Date class is a simpler operation class that gets a complete date by using a construct method that directly uses the java.util.Date class and outputting it:

public Date()

Get the system date:

import java.util.Date; public class Test{ public static void main(String[] args) { Date date = new Date();//Instantiate Date class object System.out.println("The current date is:" + date);//Output Date } }


The date style shown here does not follow the usual format. To display the time in the user's own format, use the Calendar class.

2. Calendar classes

Calendar itself is an abstract class, so it must rely on the polymorphism of the object to instantiate its parent through its subclasses, which are the Gregorian Calendar class.

Constants in the Calendar class:

The methods provided in the Calendar class:

import java.util.Calendar; import java.util.GregorianCalendar; public class Test{ public static void main(String[] args) { Calendar calendar = null;//Declare a Calendar object calendar = new GregorianCalendar();//Instantiate it through subclasses System.out.println("Year:" + calendar.get(Calendar.YEAR)); System.out.println("Month:" + calendar.get(Calendar.MONTH)); System.out.println("Day:" + calendar.get(Calendar.DAY_OF_MONTH)); System.out.println("When:" + calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("Score:" + calendar.get(Calendar.SECOND)); System.out.println("Seconds:" + calendar.get(Calendar.MILLISECOND)); } }

3. DateFormat class

In order to make the display format conform to Chinese custom requirements, you can format this type of display

Both the DateFormat and MessageFormat classes are subclasses of the Fromat class and are designed for formatting data use.

public abstract class DateFormat extends Fromat

You can see that the DateFormat class is an abstract class and therefore cannot be instantiated directly, but there is a static method in this abstract class that allows you to get an instance of this class directly.

import java.text.DateFormat; import java.util.Date; public class Test{ public static void main(String[] args) { DateFormat df1 = null;//Declare DateFormat class objects DateFormat df2 = null;//Declare DateFormat class objects df1 = DateFormat.getInstance();//Date of acquisition df2 = DateFormat.getDateTimeInstance();//Get Date Time System.out.println("DATE: " + df1.format(new Date()));//format date System.out.println("DATETIME: " + df2.format(new Date()));//format date } }


Although time is shown here, it is still not in a reasonable Chinese format.

import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Test{ public static void main(String[] args) { DateFormat df1 = null;//Declare DateFormat class objects DateFormat df2 = null;//Declare DateFormat class objects df1 = DateFormat.getDateInstance (DateFormat.YEAR_FIELD,new Locale("zh","CN"));//Get the date and format the date display df2 = DateFormat.getDateTimeInstance (DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));//Get date time and format System.out.println("DATE: " + df1.format(new Date()));//format date System.out.println("DATETIME: " + df2.format(new Date()));//format date } }


The date here has been formatted, but if you want to get a user-defined date format, you must complete it through the SimpleDateFormat class, a subclass of DateFormat

4. SimpleDateFormat class

Date formatting template tags:

Common methods of the SimpleDateFormat class:

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Test{ public static void main(String[] args) { String strDate = "2008-10-19 10:11:30.345";//A string that defines the date and time // Prepare the first template to extract numbers from the string String pat1 = "yyyy-MM-dd HH:mm:ss.SSS"; //Prepare the second template to change the extracted date number to the specified format String pat2 = "yyyy year MM month dd day HH time mm branch ss second SSS Millisecond"; SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);//Instantiate Template Object SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);//Instantiate Template Object Date d = null; try{ d = sdf1.parse(strDate);//Extracts the date from a given string }catch (ParseException e){//Exception handling if the provided string format is incorrect e.printStackTrace(); } System.out.println(sdf2.format(d));//Change Date to New Format } }


In the program, the first template is used to remove the date numbers represented in the string, that is, to convert the String to Date, and then the second template is used to convert the date numbers back to the new format.

V. Acquire Full Date (Example)

import java.text.SimpleDateFormat; import java.util.Date; class DateTime{ //Declare a date formatting operation object to instantiate new Date() private SimpleDateFormat sdf = null; //Get the full date in yyyy-MM-dd HH:mm:ss.SSS public String getDate(){ this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); return this.sdf.format(new Date()); } //Get the complete date in yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy dd Month HH mm mm min SSS s SSS MS public String getDateComplete(){ this.sdf = new SimpleDateFormat("yyyy year MM month dd day HH time mm branch ss second SSS Millisecond"); return this.sdf.format(new Date()); } public String getTimestamp(){//Get the timestamp: yyyyMMddHmmssSSS this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return this.sdf.format(new Date()); } } public class Test{ public static void main(String[] args) { DateTime dt = new DateTime();//Instantiate DateTime object System.out.println("System date:" + dt.getDate()); System.out.println("Chinese Date:" + dt.getDateComplete()); System.out.println("Timestamp:" + dt.getTimestamp()); } }

Nanhuai Bei'an Published 530 original articles, received 255 compliments, and received 210,000 visits+ His message board follow

2 February 2020, 19:50 | Views: 1400

Add new comment

For adding a comment, please log in
or create account

0 comments