如何将Joda-Time DateTime格式化为mm / dd / yyyy? [英] How to format Joda-Time DateTime to only mm/dd/yyyy?

查看:386
本文介绍了如何将Joda-Time DateTime格式化为mm / dd / yyyy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串 11/15/2013 08:00:00 ,我想将其格式化为 11/15 / 2013 ,什么是正确的 DateTimeFormatter 模式?

I have a string "11/15/2013 08:00:00", I want to format it to "11/15/2013", what is the correct DateTimeFormatter pattern?

我尝试了很多谷歌搜索仍然无法找到正确的模式。

I've tried many and googled and still unable to find the correct pattern.

编辑:我在寻找 Joda-Time DateTimeFormatter ,而不是Java的SimpleDateFormat ..

edit: I am looking for Joda-Time DateTimeFormatter, not Java's SimpleDateFormat..

推荐答案

Joda时间



创建 DateTimeFormatter 使用 DateTimeFormat.forPattern(String)

使用Joda时间你可以这样做:

Using Joda time you would do it like this:

String dateTime = "11/15/2013 08:00:00";
// Format for input
DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
DateTime jodatime = dtf.parseDateTime(dateTime);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy");
// Printing the date
System.out.println(dtfOut.print(jodatime));






标准Java≥ 8



Java 8引入了新的日期和时间库,可以更轻松地处理日期和时间。如果要使用标准Java版本8或更高版本,则可以使用 DateTimeFormatter 。由于您的字符串中没有时区,因此 java.time.LocalDateTime LocalDate ,否则划分时间 ZonedDateTime ZonedDate


Standard Java ≥ 8

Java 8 introduced a new Date and Time library, making it easier to deal with dates and times. If you want to use standard Java version 8 or beyond, you would use a DateTimeFormatter. Since you don't have a time zone in your String, a java.time.LocalDateTime or a LocalDate, otherwise the time zoned varieties ZonedDateTime and ZonedDate could be used.

// Format for input
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
// Parsing the date
LocalDate date = LocalDate.parse(dateTime, inputFormat);
// Format for output
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
// Printing the date
System.out.println(date.format(outputFormat));






标准Java< 8



在Java 8之前,您将使用a SimpleDateFormat java.util.Date

String dateTime = "11/15/2013 08:00:00";
// Format for input
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Parsing the date
Date date7 = dateParser.parse(dateTime);
// Format for output
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
// Printing the date
System.out.println(dateFormatter.format(date7));

这篇关于如何将Joda-Time DateTime格式化为mm / dd / yyyy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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