开发者

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

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: T开发者_开发技巧hu 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()); 


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜