简体   繁体   English

根据区域设置日期格式,然后获取日期部分

[英]Format Date according to locale and then get the date part

What I want to achieve is to convert a date in format yyyyMMdd to locale format ie yyyy/MM/dd or dd/MM/yyyy etc. I am not interested in the time part, I just require date. 我要实现的是将yyyyMMdd格式的日期转换为语言环境格式,即yyyy/MM/dddd/MM/yyyy等。我对时间部分不感兴趣,我只需要日期。

The function would take string date and return a string date in locale format. 该函数将采用字符串日期,并以语言环境格式返回字符串日期。

What I currently have is: 我目前拥有的是:

dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
convertedDate = dateFormat.parse("20120521");

Everything that I have tried after that either return me a long string with time and GMT etc, or the same string that I passed to the function. 在此之后我尝试的所有操作要么返回带有时间和GMT等的长字符串,要么返回与函数相同的字符串。

It sounds like you've already got the parsing part sorted - that's entirely separate from the formatting part. 听起来您已经对解析部分进行了排序-与格式化部分完全分开。

For formatting, I suspect you want: 对于格式化,我怀疑您想要:

DateFormat localeFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String text = localeFormat.format(convertedDate);

... experiment with SHORT , MEDIUM , LONG and FULL to see which one meets your needs best, but I suspect it'll be SHORT or MEDIUM . ...对SHORTMEDIUMLONGFULL进行试验,以SHORT最适合您需求的一个,但我怀疑它将是SHORTMEDIUM

(You can omit the second argument to getDateInstance and it will use the default locale, but personally I'd advise including it explicitly for clarity.) (您可以忽略getDateInstance的第二个参数,它将使用默认的语言环境,但为个人清楚起见,我个人建议您将其明确包含在内。)

You want to use the DateFormat built in types 您想使用内置的DateFormat类型

DateFormat df = DateFormat.getDateInstance(DateFormat.Short, Locale.getDefault()));

As per the Javadocs 根据Javadocs

Use getDateInstance to get the normal date format for that country. 使用getDateInstance获取该国家/地区的正常日期格式。 There are other static factory methods available. 还有其他静态工厂方法可用。 Use getTimeInstance to get the time format for that country. 使用getTimeInstance获取该国家的时间格式。 Use getDateTimeInstance to get a date and time format. 使用getDateTimeInstance获取日期和时间格式。 You can pass in different options to these factory methods to control the length of the result; 您可以将不同的选项传递给这些工厂方法,以控制结果的长度。 from SHORT to MEDIUM to LONG to FULL. 从短到中到长到满。 The exact result depends on the locale, but generally: 确切结果取决于语言环境,但通常:

SHORT is completely numeric, such as 12.13.52 or 3:30pm MEDIUM is longer, such as Jan 12, 1952 LONG is longer, such as January 12, 1952 or 3:30:32pm FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST. SHORT是完全数字的,例如12.13.52或3:30 pm MEDIUM更长,例如1952年1月12日LONG更长,例如1952年1月12日或3:30:32 pm FULL被完全指定,例如Tuesday, 1952年4月12日或太平洋标准时间下午3:30:42。

Here is an answer to your question: How can I format date by locale in Java? 这是您问题的答案: 如何在Java中按语言环境格式化日期?

an example: 一个例子:

DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, yourLocale);
String formattedDate = df.format(yourDate);

Also if you have to do a lot with Dates, consider using Joda: Java, getting Date without time 另外,如果您需要对Date做很多事情,请考虑使用Joda: Java,无需时间即可获取Date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM