【问题标题】:java convert milliseconds date corresponding timezone date in javajava在java中转换毫秒日期对应的时区日期
【发布时间】:2014-12-05 09:47:07
【问题描述】:

1)我需要找出用户的时区和时间。为此我使用

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)

通过使用这个我需要找出该用户1在相应时区的当前时间。

2) 一个用户 2 在该日期上传视频。我将其转换为毫秒并将其存储在数据库中。用户 1 想查看用户 2 的上传时间作为用户 1 的日期和时间对应他的时区。我该怎么做。

【问题讨论】:

    标签: java javascript jquery ajax jakarta-ee


    【解决方案1】:

    由于每次都以毫秒为单位存储,因此无需进行转换。您所追求的是以正确的格式显示时间(以毫秒为单位)。考虑以下场景:

    User2 在对应于值 t=12345678911ms 的日期上传视频(这是一个绝对值,对应于 1970 年 1 月 1 日 00:00 的偏移量,不需要更改)

    User1 想查看哪个日期和时间对应于他的时区基于的值 t。所以在设置了正确的日历/时区之后

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

    你只需要根据 user2 的值 t 在 user1 的日历上设置时间:

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

    如果现在打印日历时间,您将看到与 user2 对应的日期和时间

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

    【讨论】:

    • 但日历 user1C = Calendar.getInstance();在服务器上执行。它只显示服务器日历。它可能在美国。但用户可能在另一个位置。
    • 也许您应该改写您的问题,因为不清楚您要达到什么目标以及设置是什么?您没有在 user1 机器上执行的客户端代码吗?你如何与服务器交互?
    【解决方案2】:

    了解java.util.Date 对象是一个绝对时间点很重要。无法从该对象获取时区。 java.util.Date 后面的 long 值可以解释为以人们通常使用的方式获取日期(日期字符串)。这取决于格式化程序对象使用的时区。

    您还可以从日期字符串中获取java.util.Date 对象。结果取决于日历对象用于转换/解析的时区。

    这个例子可能会有所帮助:

    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");
        }
    
    }
    

    输出:

    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
    =============================================
    

    也许以下链接也很有趣: Wikipedia Article on Unix Time

    【讨论】:

    • 但是如何获取用户的TimeZone。通过使用日历 currentdate1 = Calendar.getInstance();时区 tz = Calendar.getInstance().getTimeZone();只获取服务器时区。如何识别客户端时区。
    • @PriyankaShaju 您可以询问用户或做一些其他的 CLIENTSIDE 魔术。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2018-07-20
    • 2012-09-12
    • 2019-10-24
    • 2015-10-06
    相关资源
    最近更新 更多