How to get current GMT and Current Local time in java?

Example

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class GMTExample {
public static void main(String[] args) throws ParseException {

SimpleDateFormat gmtDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS Z");
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String currentGMTDateString = gmtDateFormat.format(new Date());

SimpleDateFormat gmtDateFormat1 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS Z");
String currentDateString = gmtDateFormat1.format(new Date());

System.out.println("Current GMT=" + currentGMTDateString
+ " Current Local" + currentDateString);

}

}

Output

Current GMT=04-06-2014 07:18:25.939 +0000 Current Local04-06-2014 12:48:25.940 +0530
 

No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts