java转换毫秒日期对应的java时区日期
1) 我需要找出用户的时区和时间。为此,我使用java转换毫秒日期对应的java时区日期,java,javascript,jquery,ajax,jakarta-ee,Java,Javascript,Jquery,Ajax,Jakarta Ee,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.printl
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在那个日期上传视频。我将其转换为毫秒,并将其存储在数据库中。User1希望将user2的上载时间视为其时区对应的User1日期和时间。我如何才能做到这一点。由于每个时间都以毫秒为单位存储,因此无需进行转换。您所追求的是以正确的格式以毫秒为单位显示时间。考虑下面的场景: User2在与值t=1234567891ms相对应的日期上载视频(这是一个绝对值,对应于1970年1月1日00:00的偏移量,不需要更改) 用户1希望查看哪个日期和时间对应于基于其时区的值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());
理解
java.util.Date
对象是绝对时间点,这一点很重要。无法从该对象获取时区。java.util.Date
后面的长值可以按照人们通常使用的方式解释为获取日期(日期字符串)。这取决于格式化程序对象使用的时区
您还可以从日期字符串中获取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
=============================================
也许以下链接也很有趣:
但是Calendar user1C=Calendar.getInstance();在服务器上执行。它仅显示服务器日历。它可能在美国。但用户可能在另一个位置。也许你应该重新表述你的问题,因为它不清楚你想实现什么,设置是什么?您没有在user1计算机上执行的客户端代码吗?如何与服务器交互?但是如何获取用户的时区。通过使用Calendar currentdate1=Calendar.getInstance();时区tz=Calendar.getInstance().getTimeZone();仅获取服务器时区。如何识别客户端时区。@PriyankaShaju您可以询问用户或执行其他客户端魔术。