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 :)
- import 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: "); // for example enter: -5
- double gmtTimeZoneOffset = input.nextDouble();
- }
- }
// 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.
Re: enter the time zone offset to GMT and convert to current time
Quote:
Originally Posted by
Bonia
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
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 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");
}
}
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.
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 :)
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.