convert string to german date java

12,041

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

Share:
12,041

Related videos on Youtube

RudyMcDuty
Author by

RudyMcDuty

Updated on June 04, 2022

Comments

  • RudyMcDuty
    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
      JB Nizet almost 10 years
    • RudyMcDuty
      RudyMcDuty almost 10 years
      hi, 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
      Jens almost 10 years
      @RudyMcDuty what do you expect?
  • RudyMcDuty
    RudyMcDuty almost 10 years
    I 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
    SparkOn almost 10 years
    you 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
    RudyMcDuty almost 10 years
    my 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