How to convert a date to UTC - java

I need to change de format of a date to UTC format.
File file = new File();
...
file.lastModified();
I need to convert the lastModified date of a file in UTC format.

String lv_dateFormateInUTC=""; //Will hold the final converted date
SimpleDateFormat lv_formatter = new SimpleDateFormat();
lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
lv_dateFormateInUTC = lv_formatter.format(lv_localDate);
Something like that...!!

Quite simply:
Date date = new Date(file.lastModified())
This works because the long value returned by File.lastModified() represents the number of milliseconds since the epoch (00:00:00 GMT, January 1, 1970) as stated in the Javadoc. And the same is true of java.util.Date. So they are both in UTC/GMT already. When a date is converted to a string such as via Date.toString() or a DateFormat object, it's typically expressed in the local timezone, but the long value it stores is timezone-agnostic.

Related

Unable to convert UTC to IST, it is still returning me UTC only

I am trying to convert UTC Date to IST. But to my surprise, after converting everything, it is still returning me UTC only. How is it possible?
INPUT:
StartDateTimeUtc='2017-09-15T14:00:00',
EndDateTimeUtc='2017-09-15T15:00:00'
Code:
public static final String DATE_FORMATE_CURRENT = "yyyy-MM-dd'T'HH:mm:ss";
Date meetingStartDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.StartDateTimeUtc);
Date meetingEndDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.EndDateTimeUtc);
//Convert Date to String
DateFormat df = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT);
String meetinStartDateString = df.format(meetingStartDate);
String meetingEndDateString = df.format(meetingEndDate);
//Convert String Date to IST
SimpleDateFormat dftwo = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT);
dftwo.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
Date date = null;
Date datetwo = null;
try {
date = dftwo.parse(meetinStartDateString);
datetwo = dftwo.parse(meetingEndDateString);
} catch (ParseException e) {
e.printStackTrace();
}
dftwo.setTimeZone(TimeZone.getDefault());
String formattedStartDate = dftwo.format(date);
String formattedEndDate = dftwo.format(datetwo);
//Convert String Date back to Date format so that we can pass into Calendar code
Date meetingStartDateFinal = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(formattedStartDate);
Date meetingEndDateFinal = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(formattedEndDate);
OUTPUT again in UTC:
Start Date : Fri Sep 15 14:00:00 GMT+05:30 2017
End Date : Fri Sep 15 15:00:00 GMT+05:30 2017
A java.util.Date doesn't have any timezone information. It just contains one value: the number of milliseconds since unix epoch (1970-01-01T00:00Z, or January 1st 1907, at midnight in UTC).
This number of milliseconds is the same, everywhere in the world. What's is different is the corresponding date and time in each timezone. Example: right now, this millis value is 1505481835424, which corresponds, in UTC, to 2017-09-15T13:23:55.424Z. This same value corresponds to 10:23 AM in São Paulo, 18:53 in Kolkata, 14:23 in London and so on. The local date/time is different in each timezone, but the millis value is the same for everyone.
That's why you don't convert a Date itself: the millis value is the same, and there's no need to change it. What you can change is the representation of this date in different timezones.
SimpleDateFormat, by default, uses the JVM default timezone to parse dates. But if you know that the inputs are in a specific zone, you must set in the formatter. So, to parse your inputs, you must do:
String startDateTimeUtc = "2017-09-15T14:00:00";
String endDateTimeUtc = "2017-09-15T15:00:00";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss");
// input is in UTC
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
// parse dates
Date meetingStartDate = sdf.parse(startDateTimeUtc);
Date meetingEndDate = sdf.parse(endDateTimeUtc);
The 2 Date objects above will correspond to 14:00 and 15:00 UTC (which is the same as 19:30 and 20:30 in Kolkata timezone).
But if you just print the Date objects directly (using System.out.println, logging, or even checking their values in a debugger), it'll implicity call the toString() method, which uses the JVM default timezone behind the scenes, resulting in the output you're seeing (Fri Sep 15 14:00:00 GMT+05:30 2017).
If you want to print in a specific format, and in a specific timezone, you'll need another formatter:
// another formatter for output
SimpleDateFormat outputFormat= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
// output will be in Asia/Kolkata timezone
outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(outputFormat.format(meetingStartDate));
System.out.println(outputFormat.format(meetingEndDate));
The output will be:
2017-09-15T19:30:00
2017-09-15T20:30:00
Which corresponds to the same UTC dates in Kolkata timezone.
Just remember: you don't convert the Date's between timezones (because their millis values are "absolute" - they are the same for everyone in the world). You just change the String representation of those dates (the corresponding date/time in a specific timezone).
Java new Date/Time API
The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.
In Android you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it here).
This new API has lots of different date/time types for each situation. In this case, the inputs have date and time, but no timezone information, so first I parse them to a org.threeten.bp.LocalDateTime, using a org.threeten.bp.format.DateTimeFormatter:
// parse the inputs
DateTimeFormatter fmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime startDt = LocalDateTime.parse(startDateTimeUtc, fmt);
LocalDateTime endDt = LocalDateTime.parse(endDateTimeUtc, fmt);
Then I use a org.threeten.bp.ZoneOffset to convert them to UTC, and later a org.threeten.bp.ZoneId to convert this to another timezone. The result will be a org.threeten.bp.ZonedDateTime:
// input is in UTC
ZoneOffset utc = ZoneOffset.UTC;
// convert to Asia/Kolkata
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime start = startDt.atOffset(utc).atZoneSameInstant(zone);
ZonedDateTime end = endDt.atOffset(utc).atZoneSameInstant(zone);
Then I use the same DateTimeFormatter to format the output:
System.out.println(fmt.format(start));
System.out.println(fmt.format(end));
The output is:
2017-09-15T19:30:00
2017-09-15T20:30:00
Note that I don't need to set the timezone in the formatter, because the timezone information is in the objects (they are responsible to do the conversion).

Creating a datetime object in a particular timezone in JodaTime

I have a date, timezone and time as Strings, and I want to construct a JodaTime object with that date and time for that timezone. The code I have written is
String dateString = "2016-06-02";
String time = "01:00:00";
String timezone = "Australia/Brisbane";
DateTime dateInTimezone = DateTime.parse(dateString+" "+time,DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
.withZone(DateTimeZone.forID(timezone));
I would expect dateInTimezone to be created with value 2016-06-02T01:00:00.000+10:00 but it gets created with value 2016-06-02T18:00:00.000+10:00. My systems timezone is in America\Los_Angeles. From what I understand it takes the time I pass as a parameter in my systems timezone and converts that to the timezone I specify. Probably thats why 01:00:00 got internally converted to 18:00:00.
How do I create a joda time object with specified time and specified timezone, without any conversions?
Add the timezone to your formatter before parsing:
String dateString = "2016-06-02";
String time = "01:00:00";
String timezone = "Australia/Brisbane";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
.withZone(DateTimeZone.forID(timezone));
DateTime dateInTimezone = DateTime.parse(dateString+" "+time, formatter);
Another option is to do it your way, but call DateTime.withZoneRetainFields(), but then there's the risk of the parse failing if it falls out on an invalid date (e.g. between a DST gap) in the initial timezone.

Java exact Date to String conversion (no local GMT offset)

I'm trying to do a very simple task.
Convert a time (long myTime) in epoch seconds, to a String (but without any adjustment).
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Instant AA = Instant.ofEpochSecond(myTime);
String reportDate = df.format(Date.from(AA));
The "reportDate" is automatically adjusted with GMT offset of Windows.
I need instead the exact instant to string conversion.
I'm new on Java ....
Set the time zone specifically
df.setTimeZone(TimeZone.getTimeZone("GMT"));
This sets the offset to whatever you need so it will output the same date time on any server.

How to convert the string containing UTC time such as "193526"(19:35:26) to local time zone?

I am writing an app about GPS and I have to convert the string of UTC time obtained from NMEA format to local time.
The strings are formatted as "193526" which represents 19:35:26 UTC(GMT).
How can I convert it to local time such as "15:35:26 EDT"?
EDT TimeZone does not exist in jdk. See the answer here.
You can do the conversion this way:
SimpleDateFormat formatUTC = new SimpleDateFormat("HHmmss");
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatUTC.parse("193526");
SimpleDateFormat formatEDT = new SimpleDateFormat("HH:mm:ss z");
formatEDT.setTimeZone(TimeZone.getTimeZone("GMT-04:00"));
System.out.println(formatEDT.format(date));
15:35:26 GMT-04:00

after parsing timestamp its adding milliseconds in timestamp in java

I want to convert the string time to Timestamp Object
My code for parsing is like this
String ts = "120918 10:35:45";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd hh:mm:ss");
java.util.Date parsedDate = dateFormat.parse(ts);
//parsing timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("timestamp after parsing :: "+timestamp);
It gives me result :-- timestamp after parsing :: 2012-09-18 10:35:45.0
But I do not want milliseconds part. I want only this -- 2012-09-18 10:35:45
Please help me in removing milliseconds part.
Timestamp is a container of milliseconds. The toString() is formatting it's contains based on what it thinks is best to be displayed.
If you want to format the value, you should use a date formatter and not use the value returned by the Timestamp object.
SimpleDateFormat noMilliSecondsFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(noMilliSecondsFormatter.format(timestamp));
nb. The value you have (after you've converted it) does not contain any milliseconds anyway...
My guess, probably you didn't use "noMilliSecondsFormatter" in println().
The method java.util.Date.getTime() according to its javaDoc:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object
Therefore, when you create the TimeStamp object, you are already passing mileseconds.

Categories