如何打印没有时区的日期? [英] How can I print a Date without timezone?

查看:169
本文介绍了如何打印没有时区的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个移动应用程序,我捕获一个日期/时间,并以毫秒的形式发送到一个servlet。 - 例如我在手机上捕获12:55 AM并将其发送到我的服务器 -



所以,我从发送的毫秒构造日期并使用SimpleDateFormat打印它,时间是不同的,它打印8:55 AM。
我知道问题是因为我使用2个不同的时区,我的问题是:
如何显示该时间没有应用任何时区显示同一时间?



提前感谢

解决方案

您需要使用日历来更改TimeZone,但是没有API 。

  //更改另一个时区的日期
public static Date changeTimeZone(Date date,TimeZone zone){
日历第一= Calendar.getInstance(zone);
first.setTimeInMillis(date.getTime());

日历输出= Calendar.getInstance();
output.set(Calendar.YEAR,first.get(Calendar.YEAR));
output.set(Calendar.MONTH,first.get(Calendar.MONTH));
output.set(Calendar.DAY_OF_MONTH,first.get(Calendar.DAY_OF_MONTH));
output.set(Calendar.HOUR_OF_DAY,first.get(Calendar.HOUR_OF_DAY));
output.set(Calendar.MINUTE,first.get(Calendar.MINUTE));
output.set(Calendar.SECOND,first.get(Calendar.SECOND));
output.set(Calendar.MILLISECOND,first.get(Calendar.MILLISECOND));

return output.getTime();
}

链接: http://blog.vinodsingh.com/2009/03/date-and-timezone-in-java.html



我认为这应该是正常的。我没有经过广泛的测试。


I have a mobile application where I capture a date/time and send it as miliseconds to a servlet. -For example I capture 12:55 AM in my cellphone and send it to my server-

So, I construct the date from the sent miliseconds and print it with SimpleDateFormat but the time is diferent, it prints 8:55 AM. I know that the problem is because I use 2 diferent timezones, my question is: how can I show that time without apply any timezone to show the same time?

Thanks in advance

解决方案

You need to use Calendar to change the TimeZone but there is no API for that.

// Change a date in another timezone
public static Date changeTimeZone(Date date, TimeZone zone) {
    Calendar first = Calendar.getInstance(zone);
    first.setTimeInMillis(date.getTime());

    Calendar output = Calendar.getInstance();
    output.set(Calendar.YEAR, first.get(Calendar.YEAR));
    output.set(Calendar.MONTH, first.get(Calendar.MONTH));
    output.set(Calendar.DAY_OF_MONTH, first.get(Calendar.DAY_OF_MONTH));
    output.set(Calendar.HOUR_OF_DAY, first.get(Calendar.HOUR_OF_DAY));
    output.set(Calendar.MINUTE, first.get(Calendar.MINUTE));
    output.set(Calendar.SECOND, first.get(Calendar.SECOND));
    output.set(Calendar.MILLISECOND, first.get(Calendar.MILLISECOND));

    return output.getTime();
}

Link: http://blog.vinodsingh.com/2009/03/date-and-timezone-in-java.html

I think this should work. I haven't extensively tested it.

这篇关于如何打印没有时区的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆