java ParseException: Unparseable date [duplicate] - java

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'

Related

Convert "2020-10-31T00:00:00Z" String Date to long [duplicate]

This question already has answers here:
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
(9 answers)
How to validate the DateTime string format "2018-01-22T18:23:00.000Z" in Java?
(2 answers)
parsing date/time to localtimezone
(2 answers)
Closed 3 years ago.
I am having Input Date as "2020-10-31T00:00:00Z". i want to parse this Date to get Long milliseconds.
Note: Converted milliseconds should be in Sydney Time (ie GMT+11).
FYI,
public static long RegoExpiryDateFormatter(String regoExpiryDate)
{
long epoch = 0;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT+11"));
Date date;
try {
date = df.parse(regoExpiryDate);
epoch = date.getTime();
} catch (ParseException e) {
System.out.println("Exception is:" + e.getMessage());
e.printStackTrace();
}
System.out.println("Converted regoExpiryDate Timestamp*************** " + epoch);
return epoch;
}
Output: 1604062800000 which gives Date as 30/10/2019 by using Epoch Converter, but in input i'm passing 31st as Date.
Can anyone please clarify this?
By doing df.setTimeZone(TimeZone.getTimeZone("GMT+11"));, you are asking the date formatter to interpret your string in the GMT+11 time zone. However, your string shouldn't be interpreted in that timezone. See that Z in the string? That stands for the GMT time zone, so you should have done this instead:
df.setTimeZone(TimeZone.getTimeZone("GMT"));
In fact, your string is in the ISO 8601 format for an Instant (or a "point in time", if you prefer). Therefore, you could just parse it with Instant.parse, and get the number of milliseconds with toEpochMilli:
System.out.println(Instant.parse("2020-10-31T00:00:00Z").toEpochMilli());
// prints 1604102400000
Warning: you shouldn't really use SimpleDateFormat anymore if the Java 8 APIs (i.e. Instant and such) are available. Even if they are not, you should use NodaTime or something like that.

java.text.ParseException: Unparseable date: "2018-07-22T14:00:00-03:00" [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Java SimpleDateFormat for time zone with a colon separator?
(11 answers)
Closed 4 years ago.
#Override
public void onBindViewHolder(AirListAdapter.AirListViewHolder holder, int position) {
final Flight flight = flightList.get(position);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(flight.getRoutePoints().get(0).getDepartureTime());
holder.departureTime.setText(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
holder.airportName.setText(flightList.get(position).getRoutePoints().get(0).getLocation().getAirport_name());
holder.airportNameDestination.setText(flightList.get(position).getRoutePoints().get(0).getDestination().getAirportName());
}
PS : Unparseable date: "2018-07-22T14:00:00-03:00" Is equals getDepartureTime.
Help me please.
Replace your SimpleDateFormat with this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
The time zone information in your string is an ISO 8601 time zone, which is represented by X. Additonally, you don't want to quote the X since you're not looking for an actual letter X.

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";

Android convert UTC Date to local timezone [duplicate]

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

Convert String in milliseconds to Date Object (JAVA) [duplicate]

This question already has answers here:
Convert millisecond String to Date in Java
(5 answers)
Closed 6 years ago.
Is there a way for me to convert a String in milliseconds to a Date object?
In my program, I have to convert a Date in MM/dd/yyyy format to milliseconds, but then I have to pass that to a Date object.
Below, dateStringFinal is a String with the format "MM/dd/yyyy" already.
Calendar dateInCal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
try {
dateInCal.setTime(sdf.parse(dateStringFinal));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dateInMilli = String.valueOf(dateInCal.getTimeInMillis());
Then I have to set a date variable
someBean.setBeginDate(dateInMilli);
But dateInMilli should be a Date object. Any ideas?
new Date(Long.valueOf(dateInMs));
However, SimpleDateFormat.parse() already returns a Date.
How about using Long.parseLong(String):
someBean.setBeginDate(Long.parseLong(dateInMilli));
You can get Date immediately from calendar with calendar.getTime().
dateInCal.getTimeInMillis() returns Long. keep it in Long instead of String. And then use Date constructor that takes Long:
Long dateInMilli = dateInCal.getTimeInMillis();
someBean.setBeginDate(new Date(dateInMilli));

Categories