简体   繁体   English

在菲律宾设置正确的特定时区

[英]Setting the correct specific Time Zone in the Philippines

so I have a DateFormat function in my android app which calculate and show the date and time of the "data" that has been posted on my database.所以我在我的 android 应用程序中有一个 DateFormat function ,它计算并显示已发布在我的数据库上的“数据”的日期和时间。 Surely enough, It shows on my app, but not the correct time.果然,它显示在我的应用程序上,但不是正确的时间。 "GMT" isn't the right one, and "GMT+8" doesn't work, i also tried "Asia/Manila" but with no avail. “格林威治标准时间”不是正确的,“格林威治标准时间+8”不起作用,我也尝试了“亚洲/马尼拉”但无济于事。 Any help is appreciated.任何帮助表示赞赏。

  private String calculateTimeAgo(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("Manila"));
    try{
        long time = sdf.parse(date).getTime();
        long now = System.currentTimeMillis();
        CharSequence ago =
                DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS);
        return ago+"";
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }
    return "";
}

you can use Instant data type.您可以使用即时数据类型。 It stores the date and time in UTC format, and then converts it into LocalDateTime with timeZone.它以 UTC 格式存储日期和时间,然后将其转换为带有 timeZone 的 LocalDateTime。

for example:例如:

    Instant instantDateTime = Instant.now(); // 2022-07-28T05:46:07.652147008Z
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instantDateTime, ZoneOffset.UTC); //2022-07-28T05:46:07.652147008
    LocalDate localDate = LocalDateTime.ofInstant(instantDateTime, ZoneOffset.UTC).toLocalDate(); //2022-07-28

Now you get your local date and time.现在你得到你的本地日期和时间。 I think this can help you, Thank you.我想这可以帮助你,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM