簡體   English   中英

Java簡單日期格式解析-返回錯誤的日期時區問題?

[英]Java Simple Date Format Parse - returning wrong date Time Zone issue?

抱歉,我覺得我花了太長時間,感到困惑。

我有以下代碼:

    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時間的代碼。 如果有人可以提出更好的解決方案,我將不勝感激。 當本地時間是指定時間中的一個時,計時器應啟動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()); 

嘗試

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

原因是在給定的時間內沒有BST “英國夏令時開始於三月的最后一個星期日,結束於十月的最后一個星期日。” 因此,在一月份是格林尼治標准時間-將您的時間移到夏季,一切都會好起來的:)。

盡管有些混亂,但這是一個很好的問題,謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM