How To Convert Local Fourth Dimension To Gmt Inwards Coffee - Representative Tutorial
Converting local fourth dimension into GMT or whatever other timezone inward Java is tardily every bit Java has back upwards for fourth dimension zones. JDK has a cast called java.util.Timezone which represents timezone as well as Java likewise has classes similar SimpleDateFormat which tin purpose Time zone piece parsing or formatting dates. By using java.uti.TimeZone as well as java.text.SimpleDateFormat nosotros tin write unproblematic Java programme to convert local fourth dimension to GMT or whatever other fourth dimension zone inward Java. We bring already seen event of How to teach electrical current appointment as well as fourth dimension inward GMT as well as this Java programme is extension of that inward fellowship to convert fourth dimension cast i timezone to other timezone.
This article is inward continuation of my previous basic Java tutorials similar How to convert Double to String inward Java , How to convert char to String inward Java as well as how to write hashcode inward Java. If yous haven’t read them already yous may uncovering them useful.
Java programme to convert local fourth dimension to GMT or other Timezone
Here is consummate code event of converting local fourth dimension into GMT timezone. As I said yous tin convert local fourth dimension to whatever other timezone past times simply setting electrical current timezone for SimpleDateFormat every bit shown inward this example. By the agency beware piece using SimpleDateFormat in multi-threading surround every bit SimpleDateFormat inward Java is non thread safe.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Simple Java programme to convert local fourth dimension into GMT or whatever other TimeZone inward Java
* SimpleDateFormat inward Java tin last used to convert Date from i timezone to other
* @author Javin
*/
public class TimeZoneConverter {
public static void main(String args[]) {
//Date volition provide local fourth dimension inward Java
Date localTime = new Date();
//creating DateFormat for converting fourth dimension from local timezone to GMT
DateFormat converter = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss");
//getting GMT timezone, yous tin teach whatever timezone e.g. UTC
converter.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("local fourth dimension : " + localTime);;
System.out.println("time inward GMT : " + converter.format(localTime));
}
}
Output:
local fourth dimension : Midweek April 11 05:48:16 VET 2012
fourth dimension inward GMT : 11/04/2012:10:18:16
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Simple Java programme to convert local fourth dimension into GMT or whatever other TimeZone inward Java
* SimpleDateFormat inward Java tin last used to convert Date from i timezone to other
* @author Javin
*/
public class TimeZoneConverter {
public static void main(String args[]) {
//Date volition provide local fourth dimension inward Java
Date localTime = new Date();
//creating DateFormat for converting fourth dimension from local timezone to GMT
DateFormat converter = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss");
//getting GMT timezone, yous tin teach whatever timezone e.g. UTC
converter.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("local fourth dimension : " + localTime);;
System.out.println("time inward GMT : " + converter.format(localTime));
}
}
Output:
local fourth dimension : Midweek April 11 05:48:16 VET 2012
fourth dimension inward GMT : 11/04/2012:10:18:16
That's all on how to convert local fourth dimension into GMT or whatever other timezone inward Java similar UTC or EDT etc. piece converting fourth dimension cast i timezone to simply about other simply boot the bucket on an oculus on 24-hour interval low-cal saving every bit simply about Timezone falls nether 24-hour interval low-cal fourth dimension saving zone.
Further Learning
Complete Java Masterclass
Difference betwixt outset as well as run method of Thread inward Java

Komentar
Posting Komentar