简体   繁体   English

java在java中转换毫秒日期对应的时区日期

[英]java convert milliseconds date corresponding timezone date in java

1)I need to find out the user timezone and time. 1)我需要找出用户的时区和时间。 For that I use 为此,我用

Calendar currentdate1 = Calendar.getInstance();
TimeZone tz = Calendar.getInstance().getTimeZone(); 
System.out.println("time zone"+tz);
System.out.println(tz.getDisplayName()); // (Now India Standard Time)  
System.out.println(tz.getID()); // (Now . Asia/Kolkata)

By using this I need to find out the current time of that user1 at corresponding timezone. 通过使用此方法,我需要找出相应时区的该user1的当前时间。

2)One user2 upload video on there date. 2)一个用户2在该日期上传视频。 I convert that into millisecond and store it database. 我将其转换为毫秒,并将其存储在数据库中。 User1 want to see the uploading time of user2 as user1 date and time corresponding to his timezone.How can I do this. User1希望将user2的上传时间视为与他的时区相对应的user1日期和时间。我该如何做。

Since every time is stored in milliseconds there is no need to do a conversion. 由于每次存储的时间都以毫秒为单位,因此无需进行转换。 What you are after is presenting the time in milliseconds in the proper format. 您所追求的是以正确的格式显示时间(以毫秒为单位)。 Consider the following scenario: 请考虑以下情形:

User2 uploads a video on a date that corresponds to the value t=12345678911ms (this is an absolute value that corresponds to an offset from January 1, 1970, 00:00 and does not need to change) User2在与日期t = 12345678911ms对应的日期上传视频(这是一个绝对值,与1970年1月1日00:00的偏移量相对应,不需要更改)

User1 wants to see which date and time correspond to the value t based on his timezone. User1希望根据自己的时区查看哪个日期和时间对应于值t。 So after setting up the proper calendar/timezone 因此,在设置正确的日历/时区之后

Calendar user1C = Calendar.getInstance();//Executed on the system of user1 this command will get the correct locale and timezone:

You just have to set the time on the calendar of user1 based on the value t of user2: 您只需要基于user2的值t在user1的日历上设置时间:

user1C.setTimeInMillis(t);//Sets the the time of the current calendar based on the value of user2

If now you print the calendar time you will see the date and the time that corresponds to user2 如果现在打印日历时间,您将看到与user2对应的日期和时间

System.out.println(user1C.getTime());

It is important to understand, that a java.util.Date object is an absolute point in time. 重要的是要理解, java.util.Date对象是绝对时间点。 There is no way to get a timezone from that object. 无法从该对象获取时区。 The long value behind java.util.Date can be interpreted to get a date (date string) the way people normally use it. 可以解释java.util.Date后面的long值来获取人们通常使用的日期(日期字符串)。 This depends on the timezone the formatter object uses. 这取决于格式化程序对象使用的时区。

You can also get a java.util.Date object from an date string. 您还可以从日期字符串中获取java.util.Date对象。 The result depends on the timezone the calendar object uses for transformation/parsing. 结果取决于日历对象用于转换/解析的时区。

This example might help: 该示例可能会有所帮助:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;


public class Test {

    public static void main(final String[] args) {
        Date now = new Date();
        printDateForBerlinAndKolkata(now, "Current Time:");

        Calendar cal1 = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"));
        Calendar cal2 = new GregorianCalendar(TimeZone.getTimeZone("Asia/Kolkata"));

        cal1.set(2014, 11, 5, 15, 30);
        printDateForBerlinAndKolkata(cal1.getTime(), "Gregorian Calendar 2014-12-05 15:30 Timezone for Berlin:");

        cal2.set(2014, 11, 5, 15, 30, 0);
        printDateForBerlinAndKolkata(cal2.getTime(), "Gregorian Calendar 2014-12-05 15:30 Timezone for Kolkata:");
    }

    private static void printDateForBerlinAndKolkata(Date now, String headline) {
        System.out.println(headline);
        System.out.println("---------------------------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ssZ");
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        System.out.println("Europe/Berlin: " + sdf.format(now));
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
        System.out.println("Asia/Kolkata:  " + sdf.format(now));
        System.out.println("long value:    " + now.getTime());
        System.out.println("=============================================\n");
    }

}

Output: 输出:

Current Time:
---------------------------------------------
Europe/Berlin: 2014 Dez 05 16:35:21+0100
Asia/Kolkata:  2014 Dez 05 21:05:21+0530
long value:    1417793721481
=============================================

Gregorian Calendar 2014-12-05 15:30 Timezone for Berlin:
---------------------------------------------
Europe/Berlin: 2014 Dez 05 15:30:21+0100
Asia/Kolkata:  2014 Dez 05 20:00:21+0530
long value:    1417789821506
=============================================

Gregorian Calendar 2014-12-05 15:30 Timezone for Kolkata:
---------------------------------------------
Europe/Berlin: 2014 Dez 05 11:00:00+0100
Asia/Kolkata:  2014 Dez 05 15:30:00+0530
long value:    1417773600506
=============================================

Perhaps the following link is also of interest: Wikipedia Article on Unix Time 也许以下链接也很有趣: Unix Time上的Wikipedia文章

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

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