How to convert a date in this format (Tue Jul 13 00:00:00 CEST 2010) to a Java Date (The string comes from an alfresco property)

后端 未结 5 621
孤街浪徒
孤街浪徒 2020-11-30 07:41

i\'m managing a date that comes from an Alfresco Properties and is in the specified (Tue Jul 13 00:00:00 CEST 2010) and i need to convert it to a Java date...i\'ve looked ar

5条回答
  •  我在风中等你
    2020-11-30 08:12

    Basically your problem is that you are using a SimpleDateFormat(String pattern) constructor, where javadoc says:

    Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale.

    And if you try using this code:

    DateFormat osLocalizedDateFormat = new SimpleDateFormat("MMMM EEEE");
    System.out.println(osLocalizedDateFormat.format(new Date()))
    

    you will notice that it prints you month and day of the week titles based on your locale.

    Solution to your problem is to override default Date locale using SimpleDateFormat(String pattern, Locale locale) constructor:

    DateFormat dateFormat = new SimpleDateFormat(
                "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    dateFormat.parse("Tue Jul 13 00:00:00 CEST 2011");
    System.out.println(dateFormat.format(new Date()));
    

提交回复
热议问题