- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to display Current Time in another Time Zone
To display the current time in another time zone, use the TimeZone class. To use it, import the following package.
import java.util.TimeZone;
Firstly, set the TimeZone.
cal.setTimeZone(TimeZone.getTimeZone("Europe/Sofia"));
Now, display the date using the Calendar object.
cal.get(Calendar.HOUR_OF_DAY) cal.get(Calendar.MINUTE) cal.get(Calendar.SECOND) cal.get(Calendar.MILLISECOND)
The following is the final example.
Example
import java.util.Calendar; import java.util.TimeZone; public class Demo { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("Europe/Sofia TimeZone..."); cal.setTimeZone(TimeZone.getTimeZone("Europe/Sofia")); System.out.println("Hour = " + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("Minute = " + cal.get(Calendar.MINUTE)); System.out.println("Second = " + cal.get(Calendar.SECOND)); System.out.println("Millisecond = " + cal.get(Calendar.MILLISECOND)); } }
Output
Europe/Sofia TimeZone... Hour = 11 Minute = 16 Second = 44 Millisecond = 354
Advertisements