convert string to german date java
Solution 1
but i still have the same output: Sun Nov 11 00:00:00 CET 2012
Try to understand the thing when you use
SimpleDateDFormat#parse() - It parses text from a string to produce a Date. and Date object in java always contains date along the time.
Javadoc says Date() - Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
and FYI Sun Nov 11 00:00:00 CET 2012 is equal to 11.11.2012
Edit: try this
public static Date convertUtilDateToSqlDate(java.util.Date date){
if(date != null && !(date.equals(""))) {
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
return sqlDate;
}
return null;
}
pass the util date you got above and this method shall return the sql format date then store the sqlformat date in mysql column-field of type Date
Solution 2
You have to use M
in you pattern, because M
is the month and m
is the minute!
String date = "11.11.2012";
SimpleDateFormat sdtF = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
^^^^
Date dareFormatiert = sdtF.parse(date);
System.out.println(dareFormatiert);
For more information see the documentation of SimpleDateFormat
Related videos on Youtube
RudyMcDuty
Updated on June 04, 2022Comments
-
RudyMcDuty almost 2 years
my problem is the following. I would like to have the german date for the string "11.11.2012".
I tried this piece of code:
String date = "11.11.2012"; SimpleDateFormat sdtF = new SimpleDateFormat("dd.mm.yyyy",Locale.GERMANY); Date dareFormatiert = sdtF.parse(date); System.out.println(dareFormatiert);
But it gives me the wrong format. "Wed Jan 11 00:11:00 CET 2012", instead of "11.11.2012".
Thank you, any help is appreciated.
-
JB Nizet almost 10 yearsmm = minutes. MM = months. docs.oracle.com/javase/7/docs/api/java/text/…
-
RudyMcDuty almost 10 yearshi, thank you! i changed it to SimpleDateFormat sdtF = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY); but i still have the same output: Sun Nov 11 00:00:00 CET 2012
-
Jens almost 10 years@RudyMcDuty what do you expect?
-
-
RudyMcDuty almost 10 yearsI know but I need the string only in this form to store it in a MySQL-database. with "Sun Nov 11 00:00:00 CET 2012" I'm not able to order the data.
-
SparkOn almost 10 yearsyou are providing a string converting to date and again want string in the same format it doesnot makes sense right? what exactly is your requirement?
-
RudyMcDuty almost 10 yearsmy requirement ist to save the string in MySQL and order the data. it doesnt work when i save a string or a date-object.I also used the MySQL dataypes DATE and DATETIME to store