SimpleDateFormat的返回解析过程中错误的日期值 [英] SimpleDateFormat returns wrong date value during parse

查看:173
本文介绍了SimpleDateFormat的返回解析过程中错误的日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我米面临一个问题:我想获得GMT时间的当前时间长。
我米用下面的code下面给出:

I m facing a problem:I want to get current time of GMT TimeZone in long. I m using the following code as given below:

  TimeZone timeZoneGmt = TimeZone.getTimeZone("GMT");
  long gmtCurrentTime = getCurrentTimeInSpecificTimeZone(timeZoneGmt);

    public static long getCurrentTimeInSpecificTimeZone(TimeZone timeZone) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(timeZone);
    long finalValue = 0;
    SimpleDateFormat sdf = new SimpleDateFormat(
            "MMM dd yyyy hh:mm:ss:SSSaaa");

    sdf.setTimeZone(timeZone);

    Date finalDate = null;

    String date = sdf.format(cal.getTime());
    try {
        finalDate = sdf.parse(date);

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    finalValue = finalDate.getTime();
    return finalValue;
}

正如给出,上述方法
而格式化结果
字符串日期= sdf.format(cal.getTime());
我m到处当前时间在格林尼治标准​​时间,但像我一样按照code解析:

As given in, above method while formatting
String date = sdf.format(cal.getTime()); I m getting correct current time in GMT but as i do parsing by following code:

finalDate = sdf.parse(日期);

日期接到了当前GMT时间改为15时35分16秒IST 2013这是我的系统当前时间。

Date got changed from current GMT time to 15:35:16 IST 2013 that is current time of my system.

我试图日历以及以另一种方式:

TimeZone timeZoneGmt=TimeZone.get("GMT"); 
Calendar calGmt = Calendar.getInstance(); 
calGmt.setTimeZone(timeZoneGmt); 
long finalGmtValue = 0; 
finalGmtValue = calGmt.getTimeInMillis(); 
System.out.println("Date......" + calGmt.getTime()); 

但仍然得到日期,我的系统周四1月23日15点58分16秒IST 2014年没有得到GMT当前时间的当前时间。

but still getting date as current time of my System Thu Jan 23 15:58:16 IST 2014 Not getting GMT current time.

推荐答案

您误解了如何日期的作品。 A 日期没有的有无的时区 - 如果你使用与Date.toString()你的总是的看到默认的时区。在日期长期价值纯粹是自Unix纪元的毫秒数。它没有时区或日历系统的任何概念

You've misunderstood how Date works. A Date doesn't have a time zone - if you use Date.toString() you'll always see the default time zone. The long value in a Date is purely the number of milliseconds since the Unix epoch: it doesn't have any concept of time zone or calendar system.

如果你想重新present在特定的时间段和日历的日期和时间,使用日历代替 - 但获取当前日期和时间作为一项长期的,你可以只用 System.currentTimeMillis的(),而这又不会有什么做的系统时区。

If you want to represent a date and time in a particular time zone and calendar, use Calendar instead - but for getting "the current date and time as a long" you can just use System.currentTimeMillis(), which again does not have anything to do with the system time zone.

此外,即使你的没有的想要做的操作就是这样,你不应该使用字符串转换进行。你不是的概念的执行任何字符串转换,那么为什么他们介绍?

Additionally, even if you did want to do manipulation like this, you shouldn't be using string conversions. You're not conceptually performing any string conversions, so why introduce them?

如果你的目的是要显示(字符串)在一个特定的时区的当前日期和时间,你应该使用这样的:

If your aim is to display (as a string) the current date and time in a particular time zone, you should just use something like:

Date date = new Date(); // This will use the current time
SimpleDateFormat format = new SimpleDateFormat(...); // Pattern and locale
format.setTimeZone(zone); // The zone you want to display in

String formattedText = format.format(date);

当日期和时间API的工作 - 尤其是坏的类似于Java 日历 / 日期 API - 这是非常的重要的,你明白到底是什么在你的系统重新presents每个值。

When working with date and time APIs - particularly bad ones like the Java Calendar/Date API - it's very important that you understand exactly what each value in your system represents.

这篇关于SimpleDateFormat的返回解析过程中错误的日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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