Android convert UTC Date to local timezone [duplicate] - java

This question already has answers here:
java date parse exception while conveting UTC to local time zone
(5 answers)
Closed 7 years ago.
I get this date string from my API : "2015-12-07T14:11:15.596Z"
But this date is in UTC format and I want to convert it in local time, how can I do it ?
I tried this :
try
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.parse(this.created_at);
}
catch (ParseException e)
{
Log.e("Error Date at Whisp", e.getMessage());
return null;
}
But it return me this error :
Unparseable date: "2015-12-07T13:21:17.996Z" (at offset 10)

your Date Format pattern is wrong. Change to:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
For more informations see the javadoc of SimpleDateFormat

The T and the Z are not in your mask
Either
created_at = created_at.replace ("T", "").replace ("Z", "");
or modifiy your mask

Related

Time Conversion in java from SGT to UTC not working for 12:35:42 [duplicate]

This question already has answers here:
12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")
(1 answer)
Convert string into LocalDateTime
(4 answers)
Closed 11 days ago.
Trying to convert Singapore Time to UTc and need the result in OffsetDateTime. The below code works for me for all scenerios except one for 12:35..... for 12:00 noon it is somehow giving wrong results. Should be giving 8th Feb 04:35 morning but instead giving 7th Feb 16:35 .
what could be the way around it
Code snippets as below
if (dbTime == null) {
return null;
}
SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateTimeFormatter.setTimeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Singapore")));
Instant instant = null;
try {
dateTimeFormatter.parse(dbTime);
instant = dateTimeFormatter.parse(dbTime).toInstant();
} catch (ParseException e) {
LOGGER.warn("Could not parse ({}) to valid DateTime ", dbTime, e);
return null;
}
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.UTC);
return offsetDateTime;
}
#Test
void canConvertSGTToUTC() {
String sqlDate = "2023-02-08 12:35:45.0";
var result = dateParser.parseToOffsetDateTime(sqlDate);
assertEquals( "2023-02-08T04:35:45Z", result.toString());
}
SGT or Asia/Singapore is local system time from the servers.
use the "HH:mm:ss" format where "H" is 24hr format.
"h" is 12hr format. should solve your problem

Java SimpleDateFormat not working [duplicate]

This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 4 years ago.
I am displaying the date and time in Android with this format:
2015-11-25 23:25:00
How can I change it to the following format?
2015-11-25
I wrote some code ,but not working correctly.
public static Date toDate(boolean isCurrentDate, String dateString) {
Date date=null;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
if (isCurrentDate)
date = new Date();
else
date = formatter.parse(dateString);
System.err.println("Printresult" + formatter.parse(formatter.format(date)));
} catch (ParseException e1) {
e1.printStackTrace();
}
System.err.println("Printresult2" + date.toString());
return date;
}
I log and result is like this
Wed Nov 25 00:00:00 GMT+00:00 2015
How i can change date format like this : ? (2015-11-25)
You need one format for parsing and another for printing if the formats differ.
The following code deals with displaying the date in the format you want.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
You could alter the beheviour of toString() method by using anonymous class and overrriding toString() method or just create named class deriving from Date, but there is no point to do it.

Exception trying to convert String to UTC date format [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 5 years ago.
I tried to convert the string date to UTC date with below Java code snippet but getting Unparseable date format exception. Please find the code below and help me fix this issue.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm-SSSZ");
String strDate= "2017-06-01T01:30-0400";
try {
Date date = formatter.parse(strDate);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
date = new Date(formatter.format(date));
System.out.println(date+"gmt");
} catch (ParseException e) {
e.printStackTrace();
}
Thanks in advance.
Cannot parse the
"yyyy-MM-dd'T'HH:mm-SSSZ"
Becuase of did not match with your
strDate= "2017-06-01T01:30-0400"
Try this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm-SSS'Z'");
String strDate= "2017-06-01T01:30-040Z";

java ParseException: Unparseable date [duplicate]

This question already has answers here:
Java Unparsable date
(4 answers)
Closed 8 years ago.
I'm trying to convert a timestamp coming from a JSON API to a relative time span string like this:
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = sdf.parse(item.getTimeStamp());
long milliseconds = date.getTime();
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
milliseconds,
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
timestamp.setText(timeAgo);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
The timestamp comes back in JSON like this: 2014-07-01T00:05:20Z
I'm throwing the exception for Unparseable date
What am I doing wrong here?
Z expects timezone value, change your pattern to and you don't need SSS since you don't have milliseconds in input
yyyy-MM-dd'T'HH:mm:ss'Z'

conversion date from string bring wrong value [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 9 years ago.
I'm using the following conversion from string to date
String attr = "2013-09-11"
Date date = null;
SimpleDateFormat parsedDate = null;
parsedDate = new SimpleDateFormat("yyyy-mm-dd");
try {
date = parsedDate.parse(attr);
} catch (ParseException e) {
}
Now the date have value Fri Jan 11 00:09:00 IST 2013
why is that? and how can I change it to bring appropriate value?
mm is for minutes, you're looking for MM for months.
parsedDate = new SimpleDateFormat("yyyy-MM-dd");
More info: SimpleDateFormat, in the Date and Time Patterns and Examples section .

Categories