Java date format (yyyy-MM-dd HH:mm:ss SSS)

The common format is: yyyy-MM-dd HH:mm:ss

Take the time of 2019-12-31 06:07:59:666 as an example:

character

meaning

Example

y (lowercase y)

Year

yyyy---->2019

M (capital M)

moon

  MM---->12

d (lowercase d)

days in january

  dd---->31

H (24-hour clock, uppercase H)

hour (0-23)

  HH---->18

h (12-hour clock, lowercase h)

hours (1-12)

  hh---->06

m (lowercase m)

point

  mm---->07

s (lowercase s)

Second

  ss---->59

S (capital S)

millisecond

 SSS---->666

Y (capital y)

Week Year

YYYY---->2020

D (capital D)

days of the year

  DD---->365

What is the difference between yyyy-MM-dd and YYYY-MM-dd?

Y stands for Week Year, indicating the year in which the current week is located. This way the year will be divided into 52/53 weeks (similar to the concept of a leap year where a week will be added every few years). Each week under Week Year only belongs to a certain year. If the first week or the last week of a certain year is a new year, some dates and years will not match the actual ones.

There are two standards for Week Year:

ISO 8601 : International standard, every week starts on Monday, and the first week of each year contains at least 4 days

Common : common standard, every week starts on Sunday, and the first week of each year contains at least 1 day

Using the ISO 8601 standard , look at the example of the last week of the year and the first week of the year separately.

The last week of 2015 is the New Year’s Eve, and the first three days of 2016 (the first four days of December 2015 default to one week, which is the week of 2015). Using YYYY, the final result is 2015, and the time seems to be rolled back.

The first week of 2020 will cross the year, and the last two days of 2019 (less than 4 days, so the default is next week, which is the week of 2020) uses YYYY and the result is 2020.

    

code validation

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo {
    public static void main(String[] args) {
        // 将指定时间转换成时间戳,再由时间戳转换成时间
        String Stime = "2015-01-01 17:17:22"; // 处理前的时间
        long Ltime = 0; // 时间戳

        System.out.println("处理前的时间:" + Stime);

        // 时间转为时间戳
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date2 = simpleDateFormat.parse(Stime);
            Ltime = date2.getTime();
            System.out.println("时间转时间戳结果:" + Ltime);
        } catch (ParseException | java.text.ParseException e) {
            e.printStackTrace();
        }

        //将时间戳转换为时间
        String res = simpleDateFormat.format(Ltime);
        System.out.println("时间戳转时间结果:" + res);
    }
}

operation result:

Time before processing: 2015-01-01 17:17:22

Time to timestamp result: 1420103842000

Time stamp to time result: 2015-01-01 17:17:22

It can be seen that the two times are the same.

After changing yyyy in the code to YYYY

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

operation result:

Time before processing: 2015-01-01 17:17:22

Time to timestamp result: 1419758242000

Time stamp to time result: 2015-12-28 17:17:22

It can be seen that the two times are not the same.

o The difference between the date format of racle and java

The date format in java is:

yyyy-MM-dd HH:mm:ss: represents converting the time to a 24-hour system, for example: 2020-01-07 13:21:55

yyyy-MM-dd hh:mm:ss: means to convert the time to 12-hour system, for example: 2020-01-07 03:24:21

The date format in oracle is (case insensitive):

yyyy-MM-dd HH24:mi:ss: represents the 24-hour clock in oracle, for example: 2020/1/7 13:21:55

yyyy-MM-dd HH:mi:ss: represents the 12-hour system in oracle, for example: 2020/1/7 9:21:55

The reason why oracle is different from java is because we know that oracle is not case-sensitive, so the expression in java that represents 24 hours and 12 hours according to the case will cause problems in oracle, and the 24-hour time in oracle Special processing is done for the sum. As shown above, 24 is added after hh, and mm is changed to mi.

Most of the content is reproduced from: The pit you must know about the date format (yyyy-MM-dd HH:mm:ss SSS)_yyyy-mm-dd hh:mm:ss.sss_37358143 blog-CSDN blog

Guess you like

Origin blog.csdn.net/xijinno1/article/details/132388123