How To Convert Local Fourth Dimension To Gmt Inwards Coffee - Example Tutorial
Thursday, November 8, 2018
Add Comment
Converting local fourth dimension into GMT or whatsoever other timezone inward Java is slow every bit Java has back upward for fourth dimension zones. JDK has a shape called java.util.Timezone which represents timezone too Java likewise has classes similar SimpleDateFormat which tin forcefulness out role Time zone piece parsing or formatting dates. By using java.uti.TimeZone too java.text.SimpleDateFormat nosotros tin forcefulness out write elementary Java programme to convert local fourth dimension to GMT or whatsoever other fourth dimension zone inward Java. We accept already seen instance of How to instruct electrical flow appointment too fourth dimension inward GMT too this Java programme is extension of that inward social club to convert fourth dimension shape 1 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 too how to write hashcode inward Java. If yous haven’t read them already yous may discovery them useful.
Java programme to convert local fourth dimension to GMT or other Timezone
Here is consummate code instance of converting local fourth dimension into GMT timezone. As I said yous tin forcefulness out convert local fourth dimension to whatsoever other timezone past times only setting electrical flow timezone for SimpleDateFormat every bit shown inward this example. By the agency beware piece using SimpleDateFormat in multi-threading surroundings 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 whatsoever other TimeZone inward Java
* SimpleDateFormat inward Java tin forcefulness out endure used to convert Date from 1 timezone to other
* @author Javin
*/
public class TimeZoneConverter {
public static void main(String args[]) {
//Date volition supply 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 forcefulness out instruct whatsoever 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 whatsoever other TimeZone inward Java
* SimpleDateFormat inward Java tin forcefulness out endure used to convert Date from 1 timezone to other
* @author Javin
*/
public class TimeZoneConverter {
public static void main(String args[]) {
//Date volition supply 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 forcefulness out instruct whatsoever 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 whatsoever other timezone inward Java similar UTC or EDT etc. piece converting fourth dimension shape 1 timezone to approximately other only continue an optic on solar daytime lite saving every bit approximately Timezone falls nether solar daytime lite fourth dimension saving zone.
Further Learning
Complete Java Masterclass
Difference betwixt source too run method of Thread inward Java
0 Response to "How To Convert Local Fourth Dimension To Gmt Inwards Coffee - Example Tutorial"
Post a Comment