Java简单日期格式解析 - 返回错误的日期时区问题?

发布于 2024-12-01 17:00:40 字数 3158 浏览 1 评论 0原文

抱歉,我想我在这方面花了太长时间并且感到困惑。

我有以下代码:

    System.out.println("Time Now: " + new Date());    

    Calendar beginningOfDay = new GregorianCalendar();

    beginningOfDay.set(Calendar.HOUR_OF_DAY, 0);
    beginningOfDay.set(Calendar.MINUTE, 0);
    beginningOfDay.set(Calendar.SECOND, 0);
    beginningOfDay.set(Calendar.MILLISECOND, 0);

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");  

    Date specifiedTime = null;
    try{
        specifiedTime = sdf.parse("20:55");     // this time is extracted from interface     
    } catch(ParseException pe){}        // complete this later

    System.out.println("specified time is: " + specifiedTime);

     Calendar runResults = new GregorianCalendar(TimeZone.getDefault());
     if (beginningOfDay.getTimeInMillis() + specifiedTime.getTime() > System.currentTimeMillis())
     {  System.out.println("specified time has not occured yet");
        runResults.setTimeInMillis(beginningOfDay.getTimeInMillis() + specifiedTime.getTime());
        System.out.println("Time to run tests is: " + runResults.getTime()); 
     }
     else
     {  System.out.println("need to run tests tomorrow");
        beginningOfDay.add(Calendar.DATE, 1);       // add a day
        runResults.setTimeInMillis(beginningOfDay.getTimeInMillis() + specifiedTime.getTime());
        System.out.println("Time to run tests is: " + runResults.getTime());  
     }

给出以下输出:

Time Now: Thu Aug 25 20:17:52 BST 2011
specified time is: Thu Jan 01 20:55:00 GMT 1970
need to run tests tomorrow
Time to run tests is: Fri Aug 26 19:55:00 BST 2011

那么为什么指定时间解析会以 19:55 BST 返回?

我尝试将 sdf(simpledateformatter)时区设置为与日历相同,但似乎不起作用。

理想情况下,我想要的是将解析的任何时间(例如“20.55”)转换为本地时区的时间,如果该时间尚未发生,则计时器设置为在那时关闭,否则它应该等到第二天。这是我试图计算计时器启动 TimerTask 的时间的代码。如果有人能提出更好的解决方案,我将非常感激。当本地时间达到指定时间时,Timer 应该启动 TimerTask。

再次感谢您的帮助。

编辑:

用它来让它工作。虽然不是很整齐

    Calendar timeNow = new GregorianCalendar();

    String timeNowString = timeNow.getTime().toString();
    String timeNowFirstBit = timeNowString.substring(0, 10); // e.g. "Sat Aug 27"
    String timeNowLastBit =  timeNowString.substring(24); // e.g. "2011"

    String userSpecifiedTime = "14:32";    
    SimpleDateFormat specTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm yyyy");
    String allNewSpecifiedTime = timeNowFirstBit + " " + userSpecifiedTime + " " +                                                                 timeNowLastBit;

    Date specifiedTime = null;
    try{
    specifiedTime = specTimeFormat.parse(allNewSpecifiedTime);
    }   catch (ParseException pe){}

     Calendar runResults = new GregorianCalendar();
     runResults.setTimeInMillis(specifiedTime.getTime());
     if (specifiedTime.getTime() > System.currentTimeMillis())
     {  System.out.println("specified time has not occured yet");          
     }
     else
     {  System.out.println("need to run tests tomorrow");
        runResults.add(Calendar.DATE, 1);       // add a day          
     }
        System.out.println("Time to run tests is: " + runResults.getTime()); 

Sorry, I think I've spent too long on this and have got confused.

I've got the following code:

    System.out.println("Time Now: " + new Date());    

    Calendar beginningOfDay = new GregorianCalendar();

    beginningOfDay.set(Calendar.HOUR_OF_DAY, 0);
    beginningOfDay.set(Calendar.MINUTE, 0);
    beginningOfDay.set(Calendar.SECOND, 0);
    beginningOfDay.set(Calendar.MILLISECOND, 0);

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");  

    Date specifiedTime = null;
    try{
        specifiedTime = sdf.parse("20:55");     // this time is extracted from interface     
    } catch(ParseException pe){}        // complete this later

    System.out.println("specified time is: " + specifiedTime);

     Calendar runResults = new GregorianCalendar(TimeZone.getDefault());
     if (beginningOfDay.getTimeInMillis() + specifiedTime.getTime() > System.currentTimeMillis())
     {  System.out.println("specified time has not occured yet");
        runResults.setTimeInMillis(beginningOfDay.getTimeInMillis() + specifiedTime.getTime());
        System.out.println("Time to run tests is: " + runResults.getTime()); 
     }
     else
     {  System.out.println("need to run tests tomorrow");
        beginningOfDay.add(Calendar.DATE, 1);       // add a day
        runResults.setTimeInMillis(beginningOfDay.getTimeInMillis() + specifiedTime.getTime());
        System.out.println("Time to run tests is: " + runResults.getTime());  
     }

Which gives the following output:

Time Now: Thu Aug 25 20:17:52 BST 2011
specified time is: Thu Jan 01 20:55:00 GMT 1970
need to run tests tomorrow
Time to run tests is: Fri Aug 26 19:55:00 BST 2011

So why is the specifiedTime parse coming back with 19:55 BST?

I've tried to set the sdf (simpledateformatter) timezone to be the same as the calendar but it doesn't seem to work.

Ideally what I want is to get whatever time is parsed (e.g. "20.55") to be converted to a time in the local timezone, and if that time has not happened yet then a Timer is set to go off at that time, otherwise it should wait until the next day. This is the code with which I am trying to calculate the time the Timer initiates the TimerTask. If anyone can suggest a better solution I would be very grateful. The Timer should initiate the TimerTask when the local time is the one in the specifiedTime.

Thanks again for any help.

EDIT:

used this to get it to work. Not very neat though

    Calendar timeNow = new GregorianCalendar();

    String timeNowString = timeNow.getTime().toString();
    String timeNowFirstBit = timeNowString.substring(0, 10); // e.g. "Sat Aug 27"
    String timeNowLastBit =  timeNowString.substring(24); // e.g. "2011"

    String userSpecifiedTime = "14:32";    
    SimpleDateFormat specTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm yyyy");
    String allNewSpecifiedTime = timeNowFirstBit + " " + userSpecifiedTime + " " +                                                                 timeNowLastBit;

    Date specifiedTime = null;
    try{
    specifiedTime = specTimeFormat.parse(allNewSpecifiedTime);
    }   catch (ParseException pe){}

     Calendar runResults = new GregorianCalendar();
     runResults.setTimeInMillis(specifiedTime.getTime());
     if (specifiedTime.getTime() > System.currentTimeMillis())
     {  System.out.println("specified time has not occured yet");          
     }
     else
     {  System.out.println("need to run tests tomorrow");
        runResults.add(Calendar.DATE, 1);       // add a day          
     }
        System.out.println("Time to run tests is: " + runResults.getTime()); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-12-08 17:00:40

原因是在给定时间内没有BST。 “英国夏令时从三月的最后一个星期日开始,到十月的最后一个星期日结束。”所以一月份是格林尼治标准时间——把你的时间移到夏天,一切都会好起来的:)。

虽然有点令人困惑,但这是一个好问题,谢谢。

The reason is that in given time there is no BST. "The British Summer Time period begins on the last Sunday of March and ends on the last Sunday of October." So in January it is GMT - move your time to summer and everything will be OK :).

Although sligthly confusing, it is a good question, thanks.

白况 2024-12-08 17:00:40

尝试

specifiedTime = new GregorianCalendar();
specifiedTime.set(Calendar.HOUR_OF_DAY, 20 );
specifiedTime.set(Calendar.MINUTE, 55 );
specifiedTime.set(Calendar.SECOND, 0);
specifiedTime.set(Calendar.MILLISECOND, 0);

System.out.println("specified time is: " + specifiedTime.getTime());

try{
  specifiedTime = sdf.parse("20:55");     // this time is extracted from interface 
  beginningOfDay.add(Calendar.MILLISECOND, specifiedTime.getTime());
} catch(ParseException pe){}        // complete this later

Try

specifiedTime = new GregorianCalendar();
specifiedTime.set(Calendar.HOUR_OF_DAY, 20 );
specifiedTime.set(Calendar.MINUTE, 55 );
specifiedTime.set(Calendar.SECOND, 0);
specifiedTime.set(Calendar.MILLISECOND, 0);

System.out.println("specified time is: " + specifiedTime.getTime());

or

try{
  specifiedTime = sdf.parse("20:55");     // this time is extracted from interface 
  beginningOfDay.add(Calendar.MILLISECOND, specifiedTime.getTime());
} catch(ParseException pe){}        // complete this later
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文