我想保存数据。我的数据库服务器位于A国,但我的客户端位于B国。保存数据时,我将使用A国时间,但在客户端查看时,我将使用B国时间。您有什么想法来实现这一点吗?
提前致谢。
最佳答案
您可以通过 UTC 来完成.
您可以通过以下方式做到这一点:
TimeZone timeZone = TimeZone.getTimeZone("UTC"); // will set the UTC time zone
Calendar calendar = Calendar.getInstance(timeZone); // get calendar instance
现在假设您要设置国家/地区US
,然后:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone); // set the timezone to date format
现在使用以下命令检查两者的区别:
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());
仍有问题,请发布。
这是一个基本示例,说明如何管理时区并在国家/地区
中显示。
关于java - 保存和查看数据时的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25781657/