What does the T in yyyy-MM-dd'T'HH:mm:ssZ mean? Why use single quotes?

background

yyyy-MM-dd'T'HH:mm:ssZWhat does the date format in Java usually mean when we see it written?

In particular, why does this T serve as a separator with single quotes on the left and right? Will these single quotes be printed?

What does this Z mean? Is it the time zone? If it is a time zone, what is the format of the output? Is it a string similar to this: +0800 or +08:00 or +8:00?

explain

yyyy-MM-dd'T'HH:mm:ssZT means string T. You don’t have to use T, but everyone is accustomed to writing it as T.

  • You can also use the letter a to separate it: yyyy-MM-dd'a'HH:mm:ssZ(output eg: 2023-09-16a15:59:01+0800)
  • You can use more letters to separate page lines: yyyy-MM-dd'abc'HH:mm:ssZ(output eg: 2023-09-16abc15:59:01+0800)

There will be no single quotes in the final printed result. These single quotes are just the form used by this date format to represent characters or strings;

This date expression cannot be used without single quotes, because if not used, it will be understood as letters with special meanings (similar to y, M, d, etc.), and an exception will be thrown at runtime.

Error example:yyyy-MM-ddTHH:mm:ssZ

Verified code

public static void main(String[] args) {
    
    
   String f = "yyyy-MM-dd'T'HH:mm:ssZ";
   SimpleDateFormat sdf = new SimpleDateFormat(f);

   String format = sdf.format(new Date());
   System.out.println(format);
}

Guess you like

Origin blog.csdn.net/w8y56f/article/details/132918473