23

我正在管理一个来自 Alfresco Properties 并且在指定日期(2010 年 7 月 13 日星期二 00:00:00 CEST 2010)的日期,我需要将其转换为 Java 日期...我环顾四周发现了数百万各种字符串到日期转换表格的帖子以及这个页面,所以我尝试了这样的事情:

private static final DateFormat alfrescoDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date dataRispostaDate = alfrescoDateFormat.parse(dataRisposta);

但它会引发异常。(异常是(SSollevata un'eccezione durante la gestione della data: java.text.ParseException: Unparseable date: "Tue Jul 13 00:00:00 CEST 2011")。

我发布完整的代码:

        try {
            QName currDocTypeQName = (QName) nodeService.getType(doc);
            log.error("QName:["+currDocTypeQName.toString()+"]");
            if (currDocTypeQName != null) {
                String codAtto = AlfrescoConstants.getCodAttoFromQName(currDocTypeQName.toString());
                log.error("codAtto:["+codAtto+"]");
                if (codAtto.equals(AlfrescoConstants.COD_IQT)){
                    List<ChildAssociationRef> risposteAssociate = nodeService.getChildAssocs(doc, AlfrescoConstants.QN_RISPOSTEASSOCIATE, RegexQNamePattern.MATCH_ALL);
                    for (ChildAssociationRef childAssocRef : risposteAssociate) {
                        // Vado a prendere il nodo
                        NodeRef risposta = childAssocRef.getChildRef();
                        String dataRisposta = (nodeService.getProperty(risposta, AlfrescoConstants.QN_DATA_RISPOSTA)).toString();
                        log.error("dataRisposta:["+dataRisposta+"]");
                        if (!dataRisposta.isEmpty()){
                            try {
                                Date dataDa = dmyFormat.parse(req.getParameter("dataDa"));
                                log.error("dataDa:["+dataDa.toString()+"]");
                                Date dataA = dmyFormat.parse(req.getParameter("dataA"));
                                log.error("dataA:["+dataA.toString()+"]");
                                Date dataRispostaDate = alfrescoDateFormat.parse(dataRisposta);
                                log.error("dataRispostaDate:["+dataRispostaDate.toString()+"]");

                                if (dataRispostaDate.after(dataDa) && dataRispostaDate.before(dataA)){
                                    results.add(doc);
                                    log.error("La data risposta  è compresa tra le date specificate");
                                }else{
                                    log.error("La data risposta non è compresa tra le date specificate");
                                }
                            } catch (ParseException e) {
                                log.error("Sollevata un'eccezione durante la gestione della data: " + e);
                                throw new RuntimeException("Formato data non valido");
                            }
                        }else{
                            log.error("La data risposta non è specificata");
                        }
                    }
                }else{
                    results.add(doc);
                }
            }
        } catch (Exception e) {
            log.error("Sollevata un'eccezione durante la gestione del codice atto nel webscript nicola: " + e);
        }

任何人都可以帮忙吗?

4

5 回答 5

31

基本上你的问题是你使用的是SimpleDateFormat(String pattern)构造函数,javadoc 说:

使用给定模式和默认语言环境的默认日期格式符号构造 SimpleDateFormat 。

如果您尝试使用此代码:

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

您会注意到它会根据您的语言环境为您打印月份和星期几的标题。

您的问题的解决方案是使用SimpleDateFormat(String pattern, Locale locale)构造函数覆盖默认日期语言环境:

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()));
于 2011-05-09T14:02:27.850 回答
7
于 2018-01-14T03:28:30.233 回答
6

根据您的评论,我相信您的财产实际上是d:dated:datetime类型。如果是这样,该属性将已经作为 java Date 对象从 Alfresco 返回。所以,你需要做的就是:

  NodeRef risposta = childAssocRef.getChildRef();
  Date dataRisposta = (Date)nodeService.getProperty(risposta, AlfrescoConstants.QN_DATA_RISPOSTA);
于 2011-05-09T14:32:54.873 回答
2

The problem is that CEST is not a timezone Java supports. You can use "CST".

The Javadoc for TimeZone notes:

Three-letter time zone IDs For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

For three/four letter timezone support I suggest you try JodaTime which may do a better job.


String dataRisposta = "Tue Jul 13 00:00:00 CST 2010";
Date dataRispostaDate = alfrescoDateFormat.parse(dataRisposta);
System.out.println(dataRispostaDate);

prints

Tue Jul 13 07:00:00 BST 2010

String[] ids = TimeZone.getAvailableIDs();
Arrays.sort(ids);
for (String id : ids) {
    System.out.println(id);
}

prints

...
CAT
CET
CNT
CST
CST6CDT
CTT
...
于 2011-05-09T12:48:44.213 回答
0

试试这个功能我有同样的问题。

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

例子:

2020 年 5 月 6 日星期三 13:01:29 EDT 即“EEE MMM dd HH:mm:ss zzz yyyy”是 mycurrentFormat

4.May.2020 即“d.MMM.yyyy”是我需要的格式

Date date = new Date();

getMyDate(date.toString(), "d.MMM.yyyy", "EEE MMM dd HH:mm:ss zzz yyyy")
于 2020-05-06T17:24:48.370 回答