“This is the 15th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

Hello everyone, I'm GW_gw. I'm glad to learn and progress with you.Copy the code

The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.

Abstract

This article mainly introduces the SimpleDateFormat class some constructors and format, parse method and concrete use.Copy the code

The SimpleDateFormat class (part 1)

1. Construction method

SimpleDateFormat(String Pattern, DateFormatSymbols formatSymbols)

SimpleDateFormat(String Pattern, DateFormatSymbols formatSymbols) constructs SimpleDateFormat from the given pattern and date symbols.

The second argument, DateFormatSymbols, is a public class that encapsulates localizable date-time formatted data, such as the name of the month, the name of the day of the week, and time zone data.

DateFormatSymbols value = new DateFormatSymbols(); DateFormatSymbols value1 = new DateFormatSymbols(Locale.ENGLISH); // The argument is a local objectCopy the code

Optional parameters:

Examples show:

 DateFormatSymbols value = new DateFormatSymbols();
 DateFormatSymbols value1 = new DateFormatSymbols(Locale.ENGLISH);
 ​
 SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'", value);
 SimpleDateFormat simpleDateFormat5 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'", value1);
 ​
 Date date4 = new Date();
 String format4 = simpleDateFormat4.format(date4);
 String format5 = simpleDateFormat5.format(date4);
 System.out.println(format4);
 System.out.println(format5);
Copy the code

1.2 SimpleDateFormat(String pattern, Locale locale)

SimpleDateFormat(String Pattern, Locale Locale) Constructs SimpleDateFormat from the given schema and the default date format notation for the given Locale.

This constructor, which I understand as a simplified version of the one above, simply passes in a Local object.

Examples show:

*/ SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'",Locale.ENGLISH); Date date3 = new Date(); String format3 = simpleDateFormat3.format(date3); System.out.println(format3);Copy the code

See above for parameter resolution.

2. SimpleDateFormat class method

2.1 the format (the Date the Date)

String format(Date Date) Converts the passed Date object to a String based on a custom format.

Source:

 public final String format(Date date)
 {
     return format(date, new StringBuffer(),
                   DontCareFieldPosition.INSTANCE).toString();
 }
Copy the code

The format used here is inherited from the DateFormat parent class, so we often use DateFormat to create SimpleDateFormat objects.

DateFormat df = new SimpleDateFormat(" YYYY "); String format = df.format(new Date()); System.out.println(format);Copy the code

2.2 the parse (String STR)

Date Parse (String STR) converts the incoming String into a Date object according to the defined format.

Source:

 public Date parse(String source) throws ParseException
 {
     ParsePosition pos = new ParsePosition(0);
     Date result = parse(source, pos);
     if (pos.index == 0)
         throw new ParseException("Unparseable date: "" + source + """ ,
             pos.errorIndex);
     return result;
 }
Copy the code

The parse method here is also inherited from the parent class.

String STR = "December 25, 2019 "; try { Date parse = df.parse(str); System.out.println(parse); } catch (ParseException e) { e.printStackTrace(); }Copy the code

3. Application instance of SimpleDateFormat

Calculate how many days old you are now.

Ideas:

  1. Calculate the length of time from birth date to standard time
  2. Calculate the length of time from now to standard time
  3. Both subtraction

Concrete implementation:

public class SimpleDateFormatTest { public static void main(String[] args) throws ParseException { DateFormat df = new SimpleDateFormat(" YYYY "); String birthday = "November 1, 2000 "; Date timeBirthday = df.parse(birthday); Date nowTime = new Date(); Long day = (nowtime.getTime () -timebirthday.getTime ()))/1000/60/60/24; System.out.println(day); }}Copy the code

conclusion

This about SimpleDateFormat class learning here, I hope to be helpful to the reader. If there is a wrong place, welcome to dig friends comment correct.