这会为我提供用户的正确本地时间吗?

huangapple go评论101阅读模式
英文:

Will this give me the correct local time of the user?

问题

我需要找到一种可靠获取用户本地时间的方法。经过研究,一个潜在的解决方案可能是:

用户所在地的星期几:

int dayOfWeek = ZonedDateTime.now(Clock.systemDefaultZone()).getDayOfWeek().ordinal();

用户所在地的实际时间:

LocalDateTime.now(Clock.systemDefaultZone()));

这些都是正确的吗?我是否正确地使用了 Clock.systemDefaultZone()?如果是这样的话,我想知道 Clock.systemDefaultZone() 如何能够神奇地找出正确的时区呢?

英文:

I need to figure out a way to get the user's local time reliably. After researching, one potential solution could be:

Day of the week in user's location:

int dayOfWeek = ZonedDateTime.now(Clock.systemDefaultZone()).getDayOfWeek().ordinal();

Actual time in the user's location:

LocalDateTime.now(Clock.systemDefaultZone()));

Is this all correct? Am I correct to be using Clock.systemDefaultZone()? If so, I wonder how Clock.systemDefaultZone() is able to magically figure out the correct time zone?

答案1

得分: 1

请注意,在您的第一个示例中,您不需要一个ZonedDateTime,一个LocalDate就足够了,因为您只关心一周中的日期,而不关心当天的时间。此外,请注意,LocalDate.now()使用系统默认时钟,您不需要显式地进行指定。

关于默认时钟,Clock.systemDefaultZone()基于系统默认时区,该时区通过查询TimeZone.getDefault()来检索:

获取Java虚拟机的默认时区。如果缓存的默认时区可用,则返回其克隆。否则,该方法将执行以下步骤来确定默认时区。

  • 如果user.timezone属性值可用,则将其用作默认时区ID。
  • 检测平台时区ID。平台时区和ID映射的来源可能因实现而异。
  • 如果给定的或检测到的时区ID未知,则使用GMT作为最后的选择。
    从ID创建的默认时区被缓存,其克隆被返回。在返回时将user.timezone属性值设置为ID。
英文:

Note that in your first example, you don't need a ZonedDateTime, a LocalDate would do since you only care about the day in the week, not the time in that day. Also note that LocalDate.now() uses the system default clock and you don't need to explicitly specifly it.

Regarding that default clock, Clock.systemDefaultZone() is based on the system default time zone, which is retrieved by querying TimeZone.getDefault():

> Gets the default TimeZone of the Java virtual machine. If the cached default TimeZone is available, its clone is returned. Otherwise, the method takes the following steps to determine the default time zone.
>- Use the user.timezone property value as the default time zone ID if it's available.
>- Detect the platform time zone ID. The source of the platform time zone and ID mapping may vary with implementation.
>- Use GMT as the last resort if the given or detected time zone ID is unknown.
The default TimeZone created from the ID is cached, and its clone is returned. The user.timezone property value is set to the ID upon return.

答案2

得分: 1

就像这样简单:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        // 本地日期
        LocalDate today = LocalDate.now();
        System.out.println(today);

        // 本地时间
        LocalTime now = LocalTime.now();
        System.out.println(now);

        // 本地日期和时间
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        // 星期几
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        System.out.println(dayOfWeek);
        System.out.println(dayOfWeek.getValue());
    }
}

**我的时区输出:**

2020-10-03
17:47:57.426833
2020-10-03T17:47:57.426943
SATURDAY
6
英文:

It's as simple as this:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Main {
	public static void main(String[] args) {
		// Local date
		LocalDate today = LocalDate.now();
		System.out.println(today);

		// Local time
		LocalTime now = LocalTime.now();
		System.out.println(now);

		// Local date and time
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt);

		// Day of week
		DayOfWeek dayOfWeek = today.getDayOfWeek();
		System.out.println(dayOfWeek);
		System.out.println(dayOfWeek.getValue());
	}
}

Output for my time-zone:

2020-10-03
17:47:57.426833
2020-10-03T17:47:57.426943
SATURDAY
6

huangapple
  • 本文由 发表于 2020年10月4日 00:36:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64186581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定