simpleDateformat not parsing when second is included - java

I am getting time in a particular format which I want to convert to another format. The below code snippet was taken from one of the stack overflow answers.
SimpleDateFormat origDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date retrivedDate = origDateFormat.parse(origFormat);
return newDateFormat.format(retrivedDate);
This code format is working as long as my input date is of the following format: 22/08/2015 14:23.
But when i try with seconds, i get the error:
Unparseable date: "06/03/2019 14:17:25"
SimpleDateFormat origDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date retrivedDate = origDateFormat.parse(origFormat);
return newDateFormat.format(retrivedDate);

Here's the javadoc of parse() method and this is what it says:
Parses text from the beginning of the given string to produce a date.
The method may not use the entire text of the given string.
So, as per the documentation, you can parse 06/03/2019 14:17:25 dates with both dd/MM/yyyy HH:mm and dd/MM/yyyy HH:mm:ss date formats. If you parse with the format that doesn't contain seconds then it will ignore the second part. Below is the working code:
String date = "06/03/2019 14:17:25";
SimpleDateFormat mmDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat ssDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(mmDateFormat.parse(date));
System.out.println(ssDateFormat.parse(date));
Below are the outputs:
Wed Mar 06 14:17:00 GMT 2019
Wed Mar 06 14:17:25 GMT 2019

Related

How to format a date in a particular format which has timezone in the output [duplicate]

This question already has answers here:
Parsing Java String into GMT Date
(5 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Java - unparseable date, need format to match "GMT-0400"
(2 answers)
Closed 5 years ago.
I have a date in string format like this. It is coming from some other souce in the below format and I cannot change that:
2025-08-08T15%3A41%3A46
I have to convert above string date in this format now:
Fri Aug 08 15:41:46 GMT-07:00 2025
So below is what I have tried:
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
Date date = dateParser.parse(decodedDate);
System.out.println(date);
And this is what it prints out on the console. It prints out PDT instead of GMT-07:00. How can I get that?
Fri Aug 08 15:41:46 PDT 2025
Also I am working with Java 7 and I can use joda-time library as well. This conversion method can be called by multiple threads.
In the desired output it is printing out GMT-07:00 so how can I get the timezone also in my code?
Update:-
How about doing this way?
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("GMT-07:00"));
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
Date date = dateParser.parse(decodedDate);
System.out.println(date.toString());
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
String decodedDate = URLDecoder.decode("2025-08-08T15%3A41%3A46", "UTF-8");
Date date = dateParser.parse(decodedDate);
//Decode the given date and convert to Date object
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z-07:00 yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(date)); // set the timezone and print in the desired format
Output:
Fri Aug 08 07:41:46 GMT-07:00 2025
Update: As suggected by KevinO, a better way to do is
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT-0700"));

Java SimpleDateFormat Unparseable date

I'm having this awkward problem with simple date format.
I'm parsing some strings from a file and want to convert them in Date object.
Strings are like
"2012-04-19 18:33:10"
so my code is:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
sdf.setLenient(false);
Date d1 = sdf.parse("2012-04-19 18:33:10");
which gives me
java.text.ParseException: Unparseable date: "2012-04-19 18:33:10"
Without the
setLenient(false)
the output date is
Sun Jan 01 18:33:10 CET 2012
which is very incorrect.
I really don't understand why.
Any help would be appreciated.
Thanks in advance
Try:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Java new Date with time stamp in date format

I have used the following
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss zzz");
Date date = new Date();
String formattedDate= df.format(date);
Date dateWithTime = df.parse(formattedDate);
i got the formatted date as string when i conver this into date i got error like
java.text.ParseException: Unparseable date: "Tue Feb 26 11:45:43 IST 2013"
How would convert to date or how i format a current date and get as date?
I think your code wouldn't throw ParseException. But it sure would definitely yield wrong output. your format should be:
"dd-MM-yyyy hh:mm:ss zzz"
Note that
MM---> Months
mm---> Minutes
Test with your code with out correcting the format:
Sat Jan 26 06:24:07 GMT 2013
Test with your code with correcting the format:
Tue Feb 26 06:20:51 GMT 2013
The date format should be as follows as shown in the exception. Change it to -
EEE MMM d hh:mm:ss z yyyy
The correct simpledateformat will be
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Please refer the link for proper date formatting and parsing
SimpleDateFormat

error parsing date

String dateString="2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
i get output as follows:
Converted string to date : Tue Jan 09 00:03:00 IST 2001
What is wrong with my code?
Use MM instead of mm for months - mm means minutes, not months.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
See the API documentation of SimpleDateFormat.

How to parse String "24-10-2012" into Date ( dd-mm-yyyy) format?

I am using jdk- 1.6.
I am try to parse String "24-10-2012" date to Date (24-10-2012) but i am getting this error:
java.text.ParseException: Unparseable date: "18-11-2012"
java.text.DateFormat.parse(DateFormat.java:354)
I am parsing like this:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
System.out.println(formatter.parse(currentDate));
prints
Wed Oct 24 00:00:00 CEST 2012
Your problem cannot be reproduced with the code you have posted.
My hypothesis: your exception is thrown from a piece of code other than the one you are accusing of the error. You could try carefully analyzing the stack trace in order to track down the real culprit.
Date in java does not hold any format. Read more...
When I run
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(date);
System.out.println(formatter.format(date));
I get
Wed Oct 24 00:00:00 BST 2012
24-10-2012
which is as I expected. Can you clarity what the problem is?
You can use this for the format "dd-mm-yyyy"
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String currentDate = "24-10-2012";
Date date = formatter.parse(currentDate);
System.out.println(formatter.format(date));
import java.util.Date;
import java.text.SimpleDateFormat;
public class SimpleFormatDate
{
public static void main(String args[]){
Date todaysDate = new java.util.Date();
// Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
// Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
}
}
output
Formatted date is ==>2008-10-10 13:03:54
Formatted date is ==>2008-10-10
Formatted date is ==>10/10/2008
Wait a second.. Why u need to parsing that if u have a right value ?
Anyway, i use this :
SimpleDateFormat oFormat = new SimpleDateFormat("yyyy-MM-dd");
String sDate = oFormat.format("24-10-2012");
it will appearing date like 2012-10-24. So if u want to parsing to dd-MM-yyyy, u just need change the format to what u want.
NB : Sorry if my english is bad. :D

Categories

Resources