java pick up CEST/CET from string [duplicate] - java

This question already has an answer here:
The date format issue from java
(1 answer)
Closed 9 years ago.
I have strings like:
02/03/07 11:13:33 CEST or
02/03/07 11:13:33 CET.
How do I parse it to a util.Date in such a way that it considers cet/cest?

You need to use SimpleDateFormat object to do it:
Date d=new SimpleDateFormat("dd/MM/yy HH:mm:ss z").parse(string);
Here z stands for the zone.

Use a SimpleDateFormat with the right format string:
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss z");
String text1 = "02/03/07 11:13:33 CEST";
String text2 = "02/03/07 11:13:33 CET";
Date d1 = df.parse(text1);
Date d2 = df.parse(text2);

Related

Reformat custom date from string [duplicate]

This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 2 years ago.
I‘m fetching dates which have the format yyyy-MM-dd.
I have these extracted as a string which would look like this:
String date = "2020-09-05";
Now I want to convert it into a EEE, d MMM yyyy format, so the result should be: Sat, 5 Sep 2020.
I tried reformatting it like this:
Date convertedDate = new SimpleDateFormat(EEE, d MMM yyyy).parse(date);
For some reason this and many tries to get around it all end up with a java.text.ParseException: Unparseable date "2020-09-05".
Can‘t I convert a date from a string like that? Is there a better way to reformat a string into other formats?
You need to make a distinction between parsing the date (turning a String into a Date or LocalDate class) and formatting a date (turning the Date or LocalDate class instance to a String).
To parse the initial string, use:
LocalDate convertedDate = LocalDate.parse(date)
To format the convertedDate, use:
String formattedDate = convertedDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"))
EDIT: I am assuming you are at least on Java 8, so you can use the newer date/time classes. See https://www.baeldung.com/java-8-date-time-intro for some more info.
You are mixing up parsing and formatting: your code is trying to parse a string given the format. You’re telling Java you’re expecting the date to contain the week name.
You need to first parse your date in the format it’s in. This will give you a date. You next need format your date with the desired format, which will give you a string:
final String dateString = "2020-09-05";
final Date date = new SimpleDateFormat("y-M-d").parse(dateString);
final String converted = new SimpleDateFormat("E, d MMM y").format(date);
ok here's the code:
String sDate1="1998-12-31";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sDate1);
System.out.println("EEE, d MMM yyyy formatted date : " + new SimpleDateFormat("EEE, d MMM yyyy").format(date));

Converting Java string to Date [duplicate]

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

Convert String to Date [duplicate]

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

Modify a date object to another format [duplicate]

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

Convert Date to String in JAVA [duplicate]

This question already has answers here:
How to convert current date into string in java?
(9 answers)
Java string to date conversion
(17 answers)
Closed 8 years ago.
I have java.util.Date 02/26/2015. I want to convert it like Feburary 26,2015 as String. How can I convert it like that ??
like what goes here ??
DateFormat formatter = new SimpleDateFormat("here");
Use this format:
DateFormat formatter = new SimpleDateFormat("EEE dd,yyyy");
As descripted in the documentation of SimpleDateFormat:
E Day name in week
d Day in month
y Year
You use SimpleDateFormat
DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy");
The format above is:
MMMM Month in year,
dd Day in month,
yyyy Year,
Please read the manual for more possibilities of how to format dates properly.
SimpleDateFormat format=new SimpleDateFormat("MMMM dd, yyyy");

Categories

Resources