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 622
孤街浪徒
孤街浪徒 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:28

    Try this function I had the same issue.

    public String getMyDate(String myDate, String requiredFormat, String mycurrentFormat) {
        DateFormat dateFormat = new SimpleDateFormat(returnFormat);
        Date date = null;
        String returnValue = "";
        try {
            date = new SimpleDateFormat(myFormat, Locale.ENGLISH).parse(myDate);
            returnValue = dateFormat.format(date);
        } catch (ParseException e) {
            returnValue = myDate;
        }
        return returnValue;
    }
    

    Example:

    Wed May 06 13:01:29 EDT 2020 i.e "EEE MMM dd HH:mm:ss zzz yyyy" is mycurrentFormat

    4.May.2020 i.e. "d.MMM.yyyy" is my requiredFormat

    Date date = new Date();
    
    getMyDate(date.toString(), "d.MMM.yyyy", "EEE MMM dd HH:mm:ss zzz yyyy")
    

提交回复
热议问题