日期格式为2016年4月22日? [英] Date format of 22nd April 2016?

查看:69
本文介绍了日期格式为2016年4月22日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日期的日期格式将是什么:
2016年4月1日
2016年4月3日
2016年4月22日

what will be the date format of dates like :
1st April,2016
3rd April,2016
22nd April,2016

是否有任何API可以解析这样的日期?

And is there any API to parse dates like that ?

重复题不包含解析日期包含1(st),2(nd),3(rd)的任何答案.我不知道如何解析1st,2nd和3rd中的st.

Edit : Dupllicate questions does not contain any answer about parsing date containg 1(st), 2(nd),3(rd).I don't know how to parse st in 1st, nd in 2nd and rd in 3rd.

推荐答案

问题是Java无法理解

The problem is that Java doesn't understand the ordinal indicator suffix, 1st, 2nd 3rd etc. You firstly need to remove this and then pass that into a DateFormat.

我还建议确保正确设置您的区域设置,因为"April"将是使用不同语言的不同字符串,这将影响软件的地理可移植性.

I'd also recommend ensuring your Locale is set correctly, as "April" will be a different string in different languages which will effect geographical portability of your software.

public static void main(String[] args) throws ParseException {
    System.out.println(parseWithOrdinals("1st April,2016"));
    System.out.println(parseWithOrdinals("3rd April,2016"));
    System.out.println(parseWithOrdinals("22nd April,2016"));
}

private static Date parseWithOrdinals(String date) throws ParseException {
    DateFormat format = new SimpleDateFormat("dd MMM,yyyy", Locale.UK);
    String corrected = date.replaceFirst("(\\d+)+.*\\s(.*)", "$1 $2");
    return format.parse(corrected);
}

这篇关于日期格式为2016年4月22日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆