Java Data Type Exercises: Print the current time in GMT
Java Data Type: Exercise-5 with Solution
Write a Java program that prints the current time in GMT.
GMT: Greenwich Mean Time (GMT) is the mean solar time at the Royal Observatory in Greenwich, London. GMT was formerly used as the international civil time standard, now superseded in that function by Coordinated Universal Time (UTC).
Test Data
Input the time zone offset to GMT: 256
Sample Solution:
Java Code:
import java.util.Scanner;
public class Exercise5 {
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Input the time zone offset to GMT: ");
long timeZoneChange = input.nextInt();
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = ((totalHours + timeZoneChange) % 24);
System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond);
}
}
Sample Output:
Input the time zone offset to GMT: 256 Current time is 5:7:51
Flowchart:
Java Code Editor :
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to convert minutes into a number of years and days.
Next: Write a Java program to compute body mass index (BMI).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How to sort an ArrayList?
Collections.sort(testList); Collections.reverse(testList);
That will do what you want. Remember to import Collections though!
Ref: https://bit.ly/32urdSe
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework