Read through the time zone and timestamp and Java-based operations in one article

important concept

1. UTC 和 UTC+8

UTC is the world standard time, UTC+8 is the standard time of the East Eighth District, and China belongs to the East Eighth District, which is Beijing time.
+8 is to add 8 hours. The time zone division diagram is as follows:

That is to say:
if UTC time is now 2023-08-08 01:00:00(1:00 am on August 8, 2023), then Beijing time is now 2023-08-08 09:00:00(9:00 am).

2. Timestamp

The timestamp is a number, starting with January 1, 1970 in UTC, which is 0, and subtracting this time from the following time to get the number of seconds.
For example, the timestamp of UTC 1970-01-01 00:00:00is 0. 1970-01-01 00:01:00The timestamp is 60 one minute later .

Why start with this time?
Because this is a convention of the UNIX operating system, it is agreed that January 1, 1970 will be the beginning of the time era, and many computer-related languages ​​and systems follow this convention.

Get the timestamp of a certain time

	@Test
	public void Timestamp(){
		LocalDateTime dateTime = LocalDateTime.of(1970, 1, 1, 0, 0, 0);// 获取时间戳(秒数)
		long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC);//设置时区后获取时间戳
		System.out.println("Timestamp: " + timestamp); //0, 时间戳开始
	}
  • LocalDateTime is a time without time zone information
  • ZoneOffset.UTC represents the time zone of UTC

Java also provides an easy way to get the timestamp of the current time.

System.currentTimeMillis()

Get the time in a time zone

ZonedDateTime is a time that needs to specify a time zone. For example, at 8 am on January 1, 1970, Beijing time, it can be expressed as follows:

ZonedDateTime utc8DateTime= ZonedDateTime.of(1970, 1, 1, 8, 0, 0,0, ZoneId.of("UTC+8"));

Because the time in the East Eighth District is 8 hours ahead of the UTC standard time, the above time is also the start time of the timestamp.

	@Test
	public void zoneTimeStamp(){
		ZonedDateTime utc8DateTime= ZonedDateTime.of(1970, 1, 1, 8, 0, 0,0, ZoneId.of("UTC+8"));
		long timestamp =utc8DateTime.toEpochSecond();
		System.out.println("Timestamp1: " + timestamp);  //0
	}

Of course, time zone time can also be converted from LocalDateTime, for example:

		LocalDateTime lcoalDateTime = LocalDateTime.of(1970, 1, 1, 8, 0, 0);
		ZonedDateTime zonedDateTime = ZonedDateTime.of(lcoalDateTime, ZoneId.of("UTC+8"));
		timestamp =zonedDateTime.toEpochSecond();
		Assert.assertEquals(0, timestamp);

formatted display of time

Use DateTimeFormatter to display time as strings in different formats, the sample code is as follows:

	
	@Test
	public void dateTimeformatter() {
		LocalDateTime lcoalDateTime = LocalDateTime.of(1970, 1, 1, 8, 0, 0);
		ZonedDateTime zonedDateTime = ZonedDateTime.of(lcoalDateTime, ZoneId.of("UTC+8"));
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 
		String lcoalDateTimeStr = formatter.format(lcoalDateTime);
		Assert.assertEquals("1970-01-01 08:00:00", lcoalDateTimeStr);
		
		String zonedDateTimeStr =  formatter.format(zonedDateTime);
		Assert.assertEquals("1970-01-01 08:00:00", zonedDateTimeStr);
	}

Get the current Beijing time

The current time can be obtained by ZonedDateTime.now():

	@Test
	public void getCurrentUtc8Time() {
        // 获取UTC+8时区
        ZoneId utc8ZoneId = ZoneId.of("UTC+8");
        // 获取当前时间
        ZonedDateTime now = ZonedDateTime.now(utc8ZoneId);
        // 格式化为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");     
        String currentTime = now.format(formatter);
        // 输出时间
        System.out.println("Current time in UTC+8: " + currentTime);
	}

If you want to get the UTC standard time, you need to modify the time zone obtained above:

ZoneId.of("UTC");

convert timestamp to time

A timestamp can be converted to time by Instant.

	@Test
	public void timeMillisToUtc() {
		long timeMillis = 0L;
		Instant instant = Instant.ofEpochMilli(timeMillis);
		LocalDateTime utcDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String utcTime = utcDateTime.format(formatter);
		Assert.assertEquals("1970-01-01 00:00:00", utcTime);
	}

Note: Timestamp has nothing to do with time

In actual development, when encountering such a scenario: the valid expiration time of the authorization Token of some applications is in the format of a timestamp, which is a long integer number, someone will ask: "This has been processed by +8 Is it time?" Obviously, this question is very unprofessional.

A timestamp corresponds to a time, that is, a time based on the UTC start time. This timestamp can be converted to a time in a different time zone, but the timestamp itself has no time zone difference. That is to say, the timestamp is 0, which corresponds to the zero o'clock of January 1, 1970 in UTC time, that is, 8:00 am on January 1, 1970.

Summarize

  • LocalDateTime is a time processing class that has nothing to do with time zones
  • ZonedDateTime is a time processing class related to time zone
  • The start time of the timestamp is 0:00 on January 1, 1970, which is 8:00 am Beijing time.
  • Timestamps are independent of timezones

online example



Guess you like

Origin blog.csdn.net/oscar999/article/details/132093892