Results 1 to 11 of 11
  1. #1
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default Still need some help.. Getting current time somewhere

    Hi guys, i'm trying to get the current time at the PDT
    i tried the following:

    Java Code:
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("PDT"), Locale.US);
    c.getDate().toString();
    But it doesn't work, i also tried removing the Locale.US, but still the same..
    the output is the same time as i have.. but it should at this time be like 9AM, while it gives me 6PM (my time)


    thanks in advance!

  2. #2
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    anyone can help me?

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Im not sure but you can try this:
    Java Code:
     Calendar now = Calendar.getInstance();
    
                String hourFormat,
                       minuteFormat,
                       secondsFormat;
    
                int hour = now.get(Calendar.HOUR),
                    minute = now.get(Calendar.MINUTE),
                    seconds = now.get(Calendar.SECOND),
                    currentYear = Integer.parseInt(yearInteger.format(now.getTime())),
                    currentMonth = Integer.parseInt(monthInteger.format(now.getTime())),
    
                timeField.setText(" " + hourFormat + ":" + minuteFormat + ":" + secondsFormat);
    IF THE ABOVE DOESN'T HELP, I THINK IT HAS SOMETHING TO DO WITH YOU COMPUTER CLOCK SETTING, SET IT TO LOCALE US

  4. #4
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    Quote Originally Posted by RichersooN View Post
    Im not sure but you can try this:
    Java Code:
     Calendar now = Calendar.getInstance();
    
                String hourFormat,
                       minuteFormat,
                       secondsFormat;
    
                int hour = now.get(Calendar.HOUR),
                    minute = now.get(Calendar.MINUTE),
                    seconds = now.get(Calendar.SECOND),
                    currentYear = Integer.parseInt(yearInteger.format(now.getTime())),
                    currentMonth = Integer.parseInt(monthInteger.format(now.getTime())),
    
                timeField.setText(" " + hourFormat + ":" + minuteFormat + ":" + secondsFormat);
    IF THE ABOVE DOESN'T HELP, I THINK IT HAS SOMETHING TO DO WITH YOU COMPUTER CLOCK SETTING, SET IT TO LOCALE US
    Thats my problem, i don't want to set my clock to something else.
    I need to get the time, i tried using Locale.US in the constructor but it didn't work.. and yours will just give me my time also..

    thanks though

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    You've missed that Date doesn't have a TimeZone -- it represents an instant in time. Date's toString() does however use the default TimeZone to format its output, as you have already observed. Nothing you can do will change that.

    Use SimpleDateFormat#setTimeZone(...) and format(...)

    db

  6. #6
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    Yep, indeed it works
    Thanks!

  7. #7
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    jojo.

    May i know which OS you have been using so far?

    Why i am asking is this , i too have been suffering the same issue in my Windows 7.
    Mak
    (Living @ Virtual World)

  8. #8
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    currently XP, but it works you jsut need to do it like this: (for my example)

    DateFormat PDTFormat = new SimpleDateFormat("HH:mm:ss");
    PDTFormat.setTimeZone(TimeZone.getTimeZone("Americ a/Los_Angeles"));

    System.out.println("time "+PDTFormat.format(c));

  9. #9
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    jojo Thanks a lot. With the help of code u have sent, I fixed my problem Thanks a lot..
    Mak
    (Living @ Virtual World)

  10. #10
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    np; glad it worked! ;)

  11. #11
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    me too so happy .... Cheers..
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 01:46 PM
  2. need help with getting current time in Millis
    By Clerek in forum New To Java
    Replies: 11
    Last Post: 01-26-2011, 06:50 AM
  3. Replies: 1
    Last Post: 08-16-2010, 11:49 AM
  4. Current Time in millisecs
    By jitman in forum New To Java
    Replies: 1
    Last Post: 03-11-2010, 06:16 PM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •