Convert string with Am/pm maker to date in Java [duplicate] - java

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

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

Categories