Results 1 to 6 of 6
  1. #1
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Thumbs up enter the time zone offset to GMT and convert to current time

    Hi, I found very interesting exercise and I want to do it!!!

    Exercise: Enter the time zone offset to GMT and display the time zone in the specified time zone.
    Below is my code so far.
    Now I am not sure how to continue. Little advice please :)

    1. import java.util.Scanner;
    2. public class ProgrammingExercises2_25
    3. {
    4. public static void main( String [ ] args )
    5. {
    6. Scanner input = new Scanner (System.in);
    7. System.out.printf("Enter the time zone offset to GMT: "); // for example enter: -5
    8. double gmtTimeZoneOffset = input.nextDouble();
    9. }
    10. }



    // for example enter: -5
    // output the current time 5:50:24

    So I thought:
    Maybe I should get current time using System.currentTimeMillies method and then substract 5 and I will get current time in that GMT offset zone.
    Last edited by Bonia; 02-24-2012 at 01:34 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: enter the time zone offset to GMT and convert to current time

    Quote Originally Posted by Bonia View Post
    So I thought:
    Maybe ...
    Don't think, and no maybes. Read the API. For the present need: what does the API for System.currentTimeMillies say, after you've found the correct spelling of the method name?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: enter the time zone offset to GMT and convert to current time

    Thank you DarrylBurke!

    System.currentTimeMillis returns the current time in milliseconds.

    In this exercise I enter the time zone offset to GMT and display the time zone in the specified time zone.
    I enter the time and then I calculate the current GMT time. I feel like my logic isn't correct.
    Please about advice.

    Below is my code what I did so far:

    i
    Java Code:
    mport java.util.Scanner;
    
    public class ProgrammingExercises2_25 
    {
    	public static void main( String [ ] args ) 
    	{
    		Scanner input = new Scanner (System.in);
    	
    		System.out.printf("Enter the time zone offset to GMT: ");
    		double gmtTimeZoneOffset = input.nextDouble();
    		
    	       // Calculated the current time
    	       long totMilliseconds = System.currentTimeMillis();
    	       long totSeconds = totMilliseconds / 1000; 
    	       long currSecond = (int)(totSeconds % 60);
    	       long totMinutes = totSeconds / 60;
    	       long currMinute = (int)(totMinutes % 60);
    	       long totHours = totMinutes / 60;
    	      long currHour = (int)(totHours % 24);
    
    	      System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
    	}
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: enter the time zone offset to GMT and convert to current time

    Well, it isn't correct.
    You;re not offsetting anything.
    You're just printing (assuming the above works) the time in UTC/GMT.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: enter the time zone offset to GMT and convert to current time

    Well so really don't know what to do. I read about System.currentTimeMillis() in the internet.
    So I do this but then I don't know what to do:
    System.out.printf("Enter the time zone offset to GMT: ");
    double gmtTimeZoneOffset = input.nextDouble();

    I did what I thought is right.
    Now I don't know.

    I need somebody to tell me which direction to go now :)
    Last edited by Bonia; 02-29-2012 at 01:23 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: enter the time zone offset to GMT and convert to current time

    So, what do you think you should do with the offset entered by the user?
    What would you do (by hand) if someone asked you what the time was at GMT-8, and your watch only gave you GMT?

    That's what you need to ask the computer to do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Time Zone Problem on Windows
    By yzaltz in forum New To Java
    Replies: 4
    Last Post: 09-29-2010, 01:38 PM
  2. getting correct time zone in HPI
    By Rudre in forum Advanced Java
    Replies: 3
    Last Post: 07-02-2010, 02:46 PM
  3. Time zone stamp
    By ddscart in forum New To Java
    Replies: 1
    Last Post: 08-02-2009, 08:41 PM
  4. time Zone
    By mohammedsiddig in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 09-07-2008, 03:38 PM
  5. Replies: 0
    Last Post: 11-06-2007, 02:16 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
  •