Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the JAVA date class Date SimpleDateFormat Calendar

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand JAVA date Date SimpleDateFormat Calendar". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

1. Date date class

1.1 Constructor for Date

1.2 Constructor exercises for Date

1.3Common method exercises for Date

2. SimpleDateFormat

2.1 Common constructors

2.2 date conversion format tool class activity

2.3 Comprehensive exercise for date conversion

3.Calendar Calendar Class

3.1 concept

3.2 Common methods

3.3 getting started case

3.4 consolidate the case

1. Date date class

Class Date represents a specific moment, accurate to milliseconds

1.1 Constructor for Date

Date () allocates a Date object to represent the time it was allocated (accurate to milliseconds)

Date (long date) assigns a Date object that represents the number of milliseconds at a specified time since standard base time

Standard base time: called "epoch", that is, 00:00:00 on January 1, 1970

1.2 Constructor exercises for Date

Create package: cn.cxy.date

Create a class: TestDate1

Package cn.cxy.date;import java.util.Date;/* this class for getting started with Date * / public class TestDate1 {public static void main (String [] args) {/ / 1. Using no-parameter construction to create Date class objects, note the guide package: import java.util.Date; Date D1 = new Date (); System.out.println (D1); / / Thu Sep 23 23:14:59 CST 2021 / / 2. Create a Date object long t = 1000mm 60min 60 with parameters long t = 1000ms 60min 60-> 1h Date 60-> 1h System.out.println D2 = new Date (t); / / count back 1 h System.out.println (D2) from standard base time 1970-1-1-00:00; / / Thu Jan 01 09:00:00 CST 1970 / / CST is China time zone, so an extra 8 hours becomes 09:00:00}

Common time abbreviations

GMT Greenwich mean time = UTC coordinated Universal time

UTC + time zone difference = local time, we are in East eighth District, east plus west minus, so

CST China Standard time: China Standard Time UT+8:00

1.3Common method exercises for Date

GetTime () returns the millisecond value represented by this Date object since 00:00:00 GMT on January 1, 1970

SetTime (long time) set time, which represents the millisecond value after 00:00:00 GMT on January 1, 1970

Create package: cn.cxy.date

Create a class: TestDate2

Package cn.cxy.date;import java.util.Date;/* this class is a common method for Date to test * / public class TestDate2 {public static void main (String [] args) {/ / 1. Create the Date object Date D1 = new Date (); / / 2. To get the millisecond value from the standard base time to the present time is a time difference System.out.println (d1.getTime ()); / / 1632404114206 / * 1000 to become floating point, / 60 to seconds, / 60 to minutes, / 60 to hours, / 24 to days, / 1000 to annual System.out.println (d1.getTime () * 1.0 / 1000 / 60 / 60 / 24 / 365) / / equals about 51, from 1970 to 2021 / 3. Printing D1 is the current time System.out.println (D1); / / Thu Sep 23 21:39:29 CST 2021 / / 4 sets a long value as the time difference in milliseconds long t = 1000 seconds 60 seconds 60; d1.setTime (t); System.out.println (D1); / / what is printed is the time after the time difference t from the standard base time} 2. SimpleDateFormat

SimpleDateFormat is often used to format and parse dates

The date and time format is specified by the date and time pattern string, and the correspondence between letters and date time is as follows:

2.1 Common constructors

SimpleDateFormat () constructs SimpleDateFormat using the date format of the default schema

SimpleDateFormat (String pattern) constructs SimpleDateFormat using the date format of a given pattern

2.2 date conversion format tool class activity

Create package: cn.cxy.date

Create a class: TestSDF.java

Package cn.cxy.date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/* this class is used to test SimpleDateFormat class * / public class TestSDF {public static void main (String [] args) throws ParseException {/ * 01 format: from Date to String*/ 1. Create a date class object that contains the current time Date d = new Date (); / / 2. Create the object of the date conversion tool class, using no-parameter construction / / SimpleDateFormat sdf = new SimpleDateFormat (); SimpleDateFormat sdf2 = new SimpleDateFormat ("yyyy/MM/dd hh:mm:ss"); / / 3. Convert the date format String s = sdf2.format (d) through the tool class object you just created; / / 4. Print the converted time string: / / default time format: 21-9-23 11:18 / / Custom time format: 2021-09-23 10:21:39 System.out.println (s); / * parse: from String to Date*/ 1. Define a time string String S2 = "22:24:03 on 2021-9-23"; / / error: ParseException parsing exception: Unparseable date indivisible date: "2021-9-23 22:24:03" / / SimpleDateFormat sdf3 = new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Use the parameter construction to create a utility object, specify the format, and note that it must be a specified format, otherwise an exception SimpleDateFormat sdf3 = new SimpleDateFormat ("yyyy/MM/dd hh:mm:ss") will be thrown; / / 3. Use tool class objects to convert dates Date D2 = sdf3.parse (S2); / / 4. Print the converted date System.out.println (d2); / / Thu Sep 23 22:24:03 CST 2021} 2.3 Comprehensive date conversion exercise

Create package: cn.cxy.date2

Create a tool class: DateUtils.java

Package cn.cxy.date2;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/* this class is used as a tool class for date classes. General tool classes require: * privatization of constructors & static member methods * / public class DateUtils {/ / 1. Create a privatized constructor private DateUtils () {} / / 2. Create a method / parameter that converts a date to a string: date object Date date conversion format: format return value type: String public static String dateToString (Date date, String format) {/ / 1. Create the specified utility class object SimpleDateFormat sdf = new SimpleDateFormat (format) by passing in the date format; / / 2. Convert the string to the corresponding date string through the tool class object, and get the converted string result String s = sdf.format (date); / * convert * / / 3. Return the final converted string result to return s;} / / 3. Create a method / parameter to convert a string to a date: string object String date conversion format: format return value type: Date public static Date stringToDate (String s, String format) throws ParseException {/ / 1. Create the specified utility class object SimpleDateFormat sdf = new SimpleDateFormat (format) by passing in the date format; / / 2. Convert the date object to the corresponding string through the tool class object, and get the converted date object Date d = sdf.parse (s); / * parse * / / 3. Return the final converted date object as the result of the method to return d;}}

Create package: cn.cxy.date2

Create a test class: TestDate.java

Package cn.cxy.date2;import java.text.ParseException;import java.util.Date;/* this class is used to customize the test class of the date tool class * / public class TestDate {public static void main (String [] args) throws ParseException {/ / 1. The creation date class object Date d = new Date (); / / 2. Call the function of the custom date utility class: convert the date object to the string String s = DateUtils.dateToString (d, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy / / 10:55:32 System.out.println (S2) on September 23, 2021; / / System.out.println (S3) on September 23, 2021; / / 10:58:21 / / 3. Call the function of the custom tool class to convert the string to the corresponding date object / / Note that the format format here must be the same as when the s string is defined! Otherwise, the conversion will report an error! Date D2 = DateUtils.stringToDate (s, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

3.1 concept

The Calendar class is an abstract class that cannot be directly instantiated, but it has a direct known subclass GregorianCalendar

It provides some methods for the conversion and operation between specific moments and calendar fields such as year, month, day and so on.

3.2 Common methods

Calendar provides a method getInstance () to get the Calendar object, whose calendar field is the current date.

Int get (int field) returns the value of a given calendar field

Abstract void add (int field, int amount) adds or subtracts a specified amount of time from a given calendar field according to the rules of the calendar.

Void set (int year, int month, int date) sets the values of calendar fields YEAR, MONTH, and DAY_OF_MONTH

3.3 getting started case

Create package: cn.cxy.date3

Create a class: TestCalendar.java

Package cn.cxy.date3;import org.junit.Test;import java.util.Calendar;/* this class is used to practice the calendar class * / public class TestCalendar {@ Test public void TestGet () {/ / 1. Get the calendar object Calendar c = Calendar.getInstance (); System.out.println (c); / / you can get a lot of information, such as MONTH=8, we are now in September, indicating that the month begins at 0 / / 2. By getting the calendar object, call get (), pass in the corresponding calendar field, and you can get the corresponding value int year = c.get (Calendar.YEAR); int month = c.get (Calendar.MONTH) + 1; int day = c.get (Calendar.DAY_OF_MONTH); System.out.println (year + "year" + month + "month" + day + "day") / / September 24, 2021} @ Test public void TestAdd1 () {/ / 1. Get the calendar object Calendar c = Calendar.getInstance (); / / 2. Add and subtract a specified number of calendar fields to get today's c.add (Calendar.YEAR,+10) 10 years later; / / 3. By getting the calendar object, call get (), pass in the corresponding calendar field, and you can get the corresponding value int year = c.get (Calendar.YEAR); int month = c.get (Calendar.MONTH) + 1; int day = c.get (Calendar.DAY_OF_MONTH); / / 4. What is printed is today, 10 years later: September 24, 2031, System.out.println (year + "year" + month + "month" + day + "day");} @ Test public void TestAdd2 () {/ / demand: 7 days after printing 1 year later / / 1. Get the calendar object Calendar c = Calendar.getInstance (); / / 2. Add and subtract a specified number of calendar fields to get today's c.add (Calendar.YEAR,+1); c.add (Calendar.DAY_OF_MONTH,+7); / / 3. By getting the calendar object, call get (), pass in the corresponding calendar field, and you can get the corresponding value int year = c.get (Calendar.YEAR); int month = c.get (Calendar.MONTH) + 1; int day = c.get (Calendar.DAY_OF_MONTH); / / 4. 7 days after printing 1 year later: October 1st, 2022 System.out.println (year + "year" + month + "month" + day + "day");} @ Test public void TestSet () {/ / 1. Get the calendar object Calendar c = Calendar.getInstance (); / / 2. Test set method c.set (2099 and 9); / / 3. By getting the calendar object, call get (), pass in the corresponding calendar field, and you can get the corresponding value int year = c.get (Calendar.YEAR); int month = c.get (Calendar.MONTH) + 1; int day = c.get (Calendar.DAY_OF_MONTH); / / 4. Print: October 1st, 2099 System.out.println (year + "year" + month + "month" + day + "day");} 3.4 consolidate the case

Create package: cn.cxy.date3

Create a class: TestCalendar.java

Requirements: users enter a year at will and output how many days there are in February of this year

Package cn.cxy.date3;import java.util.Calendar;import java.util.Scanner;/* this class is used for calendar consolidation exercises * requirements: get the number of February days in any year * / public class TestFeb {public static void main (String [] args) {/ / 1. Prompt and receive the year that the user wants to test: System.out.println ("Please enter the year you want to query:"); int year = new Scanner (System.in). NextInt (); / / 2. Get the calendar object and set the time to Calendar c = Calendar.getInstance (); c.set (year, 2,1); / / 3 on March 1st of the year the user entered. The last day of February is c.add (Calendar.DATE,-1); / / 4. To get the output of this day, int date = c.get (Calendar.DATE); System.out.println (year + "February of the year" + date + "days");}} "how to understand the JAVA date class Date SimpleDateFormat Calendar" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report