- 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
Display time zone with SimpleDateFormat(“z”) in Java
You can display timezone easily in Java using SimpleDateFormat(“z”).
Firstly, to work with SimpleDateFormat class in Java, import the following package −
import java.text.SimpleDateFormat;
Now, set the format with SimpleDateFormat(“z”) to display timezone −
Format f = new SimpleDateFormat(”z”);
Now, get the timezone in a string −
String strTimeZone = f.format(new Date());
The following is an example −
Example
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy hh:mm:s z"); System.out.println("Today's date = "+simpleformat.format(cal.getTime())); // displaying hour Format f = new SimpleDateFormat("H"); String strHour = f.format(new Date()); System.out.println("Current Hour = "+strHour); // displaying minutes f = new SimpleDateFormat("mm"); String strMinute = f.format(new Date()); System.out.println("Current Minutes = "+strMinute); // displaying seconds in two-digits f = new SimpleDateFormat("ss"); String strSeconds = f.format(new Date()); System.out.println("Current Seconds = "+strSeconds); f = new SimpleDateFormat("a"); String strMarker = f.format(new Date()); System.out.println("Current AM/PM Marker = "+strMarker); // display timezone f = new SimpleDateFormat("z"); String strTimeZone = f.format(new Date()); System.out.println("TimeZone = "+strTimeZone); } }
Output
Today's date = 26/November/2018 08:11:21 UTC Current Hour = 8 Current Minutes = 11 Current Seconds = 21 Current AM/PM Marker = AM TimeZone = UTC
Advertisements