是否可以创建一个DateFormatter,将两位数年份转换为四位数年份? [英] Is it possible to create a DateFormatter which converts a two-digit year into a four-digit year?

查看:252
本文介绍了是否可以创建一个DateFormatter,将两位数年份转换为四位数年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java应用程序中,我使用 DateFormat 实例来解析日期输入。

In my Java application I use a DateFormat instance to parse date inputs.

DateFormat fmt;
fmt = DateFormat.getDateInstance(DateFormat.DEFAULT)  // dd.MM.yyyy for de_DE

问题是用户坚持以 31.12.11 的形式输入日期。

The problem is that the user insists to enter dates in the form 31.12.11.

不幸的是,这会解析为 31.12.11 。 ( 0011-12-31 在ISO格式)而是我想要解析的日期成为 31.12.2011

Unfortunately this is parsed to 31.12.11. (0011-12-31 in ISO format) Instead I want the parsed date to become 31.12.2011 (2011-12-31 in ISO format).

我可以修改日期格式以某种方式解析输入吗?

Can I modify the date format to somehow parse inputs that way?

推荐答案

您必须以dd.MM.yy的格式解析,并以yyyy-MM- dd

You will have to parse with a format of dd.MM.yy and re-format with a format of yyyy-MM-dd

DateFormat sdfp = new SimpleDateFormat("dd.mm.yy");
Date d = sdfp.parse(input);
DateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
String date = sdff.format(d);

有关设置模式的详细信息,请参阅Java API。

See the Java API for more info on setting patterns.

http://download.oracle.com/javase/6 / docs/api/java/text/SimpleDateFormat.html

这篇关于是否可以创建一个DateFormatter,将两位数年份转换为四位数年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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