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

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

Related

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.

JAVA - Format a custom string using SimpleDateFormat [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Java string to date conversion
(17 answers)
Closed 5 years ago.
I'm having trouble formatting a custom String back to a Date object. What i have:
String customString = "October 14, 2015;
Date date = new Date();
SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy");
try {
date = s.parse(customString);
} catch (ParseException e) {
e.printStackTrace();
}
I always get a unappeasable date exception. Any pointers of what i'm doing wrong is appreciated.
Your pattern must be: new SimpleDateFormat("MMM dd,yyyy");
For more informations about SimpleDateFormat see the javadoc
Read the docs, https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
The argument of the constructor should have the format of the date you want to input.
For instance, if you want the full month name should have "MMMMM".
Just make the following change and the program will work.
SimpleDateFormat s = new SimpleDateFormat("MMMMM dd, yyyy");

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 with Am/pm maker to date in Java [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 7 years ago.
I want to convert string "10/7/2015 6:31am" to Date in java.
I tried follows code, but failed.
String value = "10/7/2015 6:31am";
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy h:mma");
return sdf.parse(value);
What SimpleDateFormat should I use, please help me
actually, I made a function as follows in Utils.java:
public static Date getDateValueByFormat(String value,String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(value);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
called this function in others class as follows:
String value = "10/7/2015 6:31am";
Date datetime = Utils.getDateValueByFormat(value, "M/d/yyyy h:mma");
unfortunately, threw excption as follows:
java.text.ParseException: Unparseable date: "10/7/2015 6:31am"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.sapmle.common.util.Utils.getDateValueByFormat(Utils.java:28)
anyone has idea?
PS: I used jdk is 1.7.0_79, has any problem for it?
Tom, prashant thakre,
Thank you very much!
I successed by your way:
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);

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'

Categories