This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I want to change date format in JAVA as in below. Please help.
-> input is of java.util date datatype (Sat Jan 20 00:00:00 IST 2018)
EX-
Calendar c = Calendar.getInstance();
Date input = c.getTime();
System.out.println(input);//prints[Sat Jan 20 00:00:00 IST 2018]
-> Output should be of java.util date datatype (2018-01-20)
EX-
Date output = null;
output = **[logic to convert input to 2018-01-20]**
System.out.print(output);//should print 2018-01-20
I am getting output in String format.
Please help me to find the output in Date format
You will need two SimpleDateFormat objects. One for parsing the input and one for formatting the output.
public static void main(String[] args) throws ParseException {
String input = "Tue Dec 20 00:00:00 IST 2005";
SimpleDateFormat inFormat = new SimpleDateFormat("EEE MMM d H:m:s zzz y");
Date d = inFormat.parse(input);
System.out.println("d = " + d);
SimpleDateFormat outFormat = new SimpleDateFormat("y-MM-d");//(2005-12-20
System.out.println("" + outFormat.format(d));
}
For a more indepth description on how to format the SimpleDateFormat object, check out the Javadocs.
Related
This question already has answers here:
How to convert Date to a particular format in android?
(7 answers)
Converting date-string to a different format
(5 answers)
Changing String date format
(4 answers)
Closed 3 years ago.
I'm having a string like this: Wed Feb 20 02:48:00 GMT+05:30 2019
and I need to convert it to Wed 20.
How can I do this?
I tried string.replace(), but that doesn't help
I'm a newbie please help
If you will be working with dates that are always represented in this String format,
then perhaps you can use the String split method, to break apart your date on the spaces.
String sourceDate = "Wed Feb 20 02:48:00 GMT+05:30 2019";
String[] dateParts = sourceDate.split(" ");
This will result in an Array containing the seperate blocks.
dateParts = { 'Wed', 'Feb', '20','02:48:00', 'GMT+05:30', '2019' }
Then you can take the blocks you need
String resultDate = dateParts[0] +" "+ dateParts[2];
Wed 20
If you intend to do other manipulations on the date, you might want to look into converting your date String to an actual Date using a DateTimeFormatter
This works like this:
String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02
Where LocalDate.parse() needs to be replaced depending on your date format.
Only the date -> LocalDate#parse(text, formatter)
Date and time -> LocalDateTime#parse(text, formatter)
Date, time and timezone -> ZonedDateTime#parse(text, formatter)
try this
class Test
{
public static void main(String ... args)
{
String s="Wed Feb 20 02:48:00 GMT+05:30 2019";
String arr[]=s.split(" ");
if (arr.length >=3)
{
String result=arr[0]+" "+arr[2];
System.out.println(result);
}
}
}
$ java Test
Wed 20
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:
Convert string dates in java [duplicate]
(3 answers)
Closed 5 years ago.
The string is of the format dd/mm/yyyy
I am parsing using the following code:-
Dateformat dateformatter = new SimpleDateFormat("EEE, MMM d, ''yy");
dateformatter.setLenient(false );
String temp = "7/07/2017"
Date date = null;
try {
date = new SimpleDateFormat("dd/mm/yyyy").parse(temp);
}
catch(ParseException e){
e.printStackTrace();
}
dateformatter.format(date);
The value that I get for date is Sat Jan 07 00:07:00 GMT + 10:00 2017
After formatting I get Sat, Jan 7, '17
The value I expect is FRI Jul 07, '17
The issue is in this line
date = new SimpleDateFormat("dd/mm/yyyy").parse(temp);
In the SimpleDateFormat, Month is denoted with M and not m.
M - Month in year (context sensitive)
m - Minute in hour
Change this to
date = new SimpleDateFormat("dd/MM/yyyy").parse(temp);
For more details, check the official documentation of SimpleDateFormat
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:
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));