2017-05-25 149 views
-2

是否有任何Java库可用于解析语言特定的序数指示符/后缀?如何将日期值“2017年5月26日”转换为“26/05/2017”?

我有如下的日期值:26th May 2017。我想将其转换为26/05/2017。任何人都可以请指导我如何做?

+4

在Java或Javascript中? Spring和Hibernate需要做什么?你在关于日期格式转换的许多问题上做了哪些研究? –

+0

我想将2017年5月26日转换为Java程序中的26/05/2017。 – Arvinda

+0

有没有更好的方法在java中做到这一点? – Arvinda

回答

1

假设你问的是Java,这个链接:https://www.mkyong.com/java/how-to-convert-string-to-date-java/可以帮助你。

总的要点是:

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class TestDateExample3 { 

    public static void main(String[] argv) { 



     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 

     String dateInString = "26th May 2017"; // Remove your 'th', 'nd', etc. from the input string. 

     String withoutEnding = dateInString; 
     //Something like this 
     if(dateInString.contains("th") withoutEnding = dateInString.replace("th", ""); 
     if(dateInString.contains("nd") withoutEnding = dateInString.replace("nd", ""); 
     if(dateInString.contains("st") withoutEnding = dateInString.replace("st", ""); 
     if(dateInString.contains("rd") withoutEnding = dateInString.replace("rd", ""); 

     try { 

      Date date = formatter.parse(withoutEnding); 
      System.out.println(date); 
      System.out.println(formatter.format(date)); 

     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

dd/MM/yyyy是一个日期格式,会给你 26/05/2017

希望这会有所帮助!

编辑:另见http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.htmlSimpleDateFormat不同模式字母的完整列表。

+0

我想用'th','nd'解析日期值为标准Java日期格式。 – Arvinda

+0

@Arvinda对不起,误解了!我只是更新了答案,以更好地回答你的实际问题。可能不是最有效的方式(我在工作,所以没有很多时间),但应该工作! –

+0

@Arvinda你甚至可以留下后缀,但我不确定,我没有测试过这个。 –

3

您可以使用自定义日期格式直接解析这种格式到Java 8 LocalDate

static final Map<Long, String> ORDINAL_DAYS = new HashMap<>(); 
static 
{ 
    ORDINAL_DAYS.put(1, "1st"); 
    .... more .... 
    ORDINAL_DAYS.put(26, "26th"); 
    .... more .... 
    ORDINAL_DAYS.put(31, "31st"); 
} 

static final DateTimeFormatter FORMAT_DAY_MONTH_YEAR = new DateTimeFormatterBuilder() 
    .appendText(ChronoField.DAY_OF_MONTH, ORDINAL_DAYS) 
    .appendLiteral(' ') 
    .appendText(ChronoField.MONTH_OF_YEAR) 
    .appendLiteral(' ') 
    .appendText(ChronoField.YEAR) 
    .toFormatter(); 


String dateInString = "26th May 2017"; 

LocalDate date = LocalDate.parse(dateInString, FORMAT_DAY_MONTH_YEAR); 

这是使用的DateTimeFormatter.appendText版本它接受用于映射一天字符串的地图。

您需要填写ORDINAL_DAYS中所有我遗漏的所有缺失条目。

+0

更宽大,但也很简短:'static final DateTimeFormatter FORMAT_DAY_MONTH_YEAR = DateTimeFormatter.ofPattern(“d ['st'] ['nd'] ['rd'] ['th'] MMMM uuuu”,Locale.english);' 。 PS考虑指定区域设置。 –

2

假设你并不需要非常严格的输入验证,因为你是从转换th上的号码(或stnd31st2nd多)的格式,我建议你只是删除这些两个字母第一。一个正则表达式可以做到这一点:

// remove st, nd, rd or th after day of month 
    dateInString 
      = dateInString.replaceFirst("^(\\d+)(st|nd|rd|th)(\\w+ \\d+)$", "$1$3"); 
    String dateOutString = LocalDate.parse(dateInString, 
        DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH)) 
      .format(DateTimeFormatter.ofPattern("dd/MM/uuuu")); 

结果是

26/05/2017 

这工作如果输入包含当月三个字母的缩写,如四月,五月或六月要接受一个完整的月份名称。相反(4月,5月,6月),格式模式中需要4个Ms而不是3个:d MMMM uuuu

2

正如@ OleV.V所述。在this comment,可以使用可选的部分的图案(解析不同的后缀STNDRD)。

您还必须使用java.util.Locale强制月份名称为英文。该代码将是这样的:

String input = "26th May 2017"; 
DateTimeFormatter parser = DateTimeFormatter 
    // parse the day followed by st, nd, rd or th (using optional patterns delimited by []) 
    .ofPattern("dd['st']['nd']['rd']['th'] MMM yyyy") 
    // force English locale to parse month names 
    .withLocale(Locale.ENGLISH); 
// formatter for dd/MM/yyyy output 
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy").withLocale(Locale.ENGLISH); 
System.out.println(formatter.format(parser.parse(input))); // 26/05/2017 

上面的代码将月份名称与工作3个字母(如)。如果要分析的全名(如八月三月),只是改变MMMMMMM

DateTimeFormatter parser = DateTimeFormatter 
    // using MMMM to parse full month name (like "August") 
    .ofPattern("dd['st']['nd']['rd']['th'] MMMM yyyy") 
    .withLocale(Locale.ENGLISH); 

PS:如果要分析这两种情况下(3个字母或全月名称)使用相同的parser,您可以这样做:

DateTimeFormatter parser = DateTimeFormatter 
    // can parse "March" or "Mar" (MMMM or MMM) 
    .ofPattern("dd['st']['nd']['rd']['th'][ MMMM][ MMM] yyyy") 
    .withLocale(Locale.ENGLISH); 
+1

这是一个很好的答案,如果我对它有所贡献或以某种方式启发它,我很高兴。如果是这样,一提就好了。无论如何,你的解释可能比我的更好,或者至少是不同的,因此是一个很好的补充。 –

+1

在这两个代码片段中,您都可以使用两个参数'DateTimeFormatter.ofPattern()'作为一个比较特殊的代码。 –

+1

@ OleV.V。说实话,我在**发布我的答案(我刚刚在这里有一个非常类似的代码,带有可选模式等等)后,才看到您对此解决方案的评论,只要我阅读问题,发布答案没有看别的东西)。对不起,如果我给人的印象是我抄袭了你的想法而没有给予任何荣誉。无论如何,我要编辑答案来添加此信息。而且我总是忘记'ofPattern'的2-arg版本 - 我通常不需要设置不同的语言环境。谢谢! – 2017-05-26 11:50:40

相关问题