This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 4 years ago.
I need to convert a java string in a date format to a new date format to send it to my sql-server. Currently, my Java date is Jul 17 2018 14:22:58, and I need it to look like 2018-07-17 14:22:58. So I created this:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
Date formattedDate = sdf.parse(oldTime);
I am getting errors that my oldTime is unparseable. Did I do this wrong?
You need to use a format string that matches your date string:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");
Related
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 4 years ago.
I am working with system's API which returns me dates in this format -
Sun Jun 10 05:23:03 2018.
I want to efficiently parse it to something that will look like this -
"dd-mm-year".
Is there any parsing built in Java I can use? Or I need to use a specific function for it?
Thanks.
DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy");
System.out.println(LocalDate.parse(str, formatter));
Output:
2018-06-10
You can parse as below with SimpleDateFormat :
public static void main(String[] args) throws IOException, ParseException {
String str = "Sun Jun 10 05:23:03 2018";
DateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = fmt.parse(str);
System.out.println(date);
System.out.println(targetFormat.format(date));
}
Outputs :
Sun Jun 10 05:23:03 EET 2018
10-06-2018
You can use SimpleDateFormat class in Java.
Following snippet will show you how to parse date accordingly.
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String strDate= formatter.format(date);
I hope this is what you are looking for. I hope this helps you.
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"));
This question already has answers here:
Month issue in SimpleDateFormat class
(6 answers)
Closed 5 years ago.
I am trying to get a Date object in the format YYYY-MM-DD representing the current system date. Below is my attempt:
Date todayDate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String todayString = formatter.format(todayDate);
The values are as follows:
todayDate: Mon May 15 16:24:47 GMT+01:00 2017
todayString: 2017-24-15
Having tried this a couple of times I noticed the todayString is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.
How might I get the current date in the YYYY-MM-DD format?
Change this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
to this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
mm stands for the minutes, while MM (capitalized) stands for the month
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
From this question and its answers, I tried to convert string to date. But it seems to strange with me.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
It returns
Mon Dec 29 11:56:00 ICT 2014
I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?
First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);
The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.
Try this:
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
I downloaded date information from an API which is in the format of (Java)
day month monthnumber hour:minute:second EST year
how can I format this into just
month monthnumber year
You could use SimpleDateFormat to first parse the string to a java.util.Date, and then format it to the format you want:
String orig = "Thu Feb 19 19:40:12 EST 2015";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = parser.parse(orig);
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
String formatted = formatter.format(date);