如何在JodaTime中将yyyymmdd格式的字符串转换为LocalDate [英] How to convert a String of format yyyymmdd to LocalDate in JodaTime

查看:634
本文介绍了如何在JodaTime中将yyyymmdd格式的字符串转换为LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,格式为"20140518".如何将其转换为LocalDate对象

I have a String in the form "20140518". How to convert it into LocalDate object

我尝试过

this.todayDate = new LocalDate(val);
System.out.println(todayDate.toString("yyyy-mm-dd"))

当我尝试将其转储到标准输出时,它会像20140518-junk-junk一样转储.它转储了一个垃圾字符串.我以为它会像2014-05-18那样丢弃.

When I try dumping this to standard output it dumps like 20140518-junk-junk. That it dumps a garbage string . I thought it would dump like 2014-05-18.

推荐答案

使用表示月的MM而不是表示分钟的mm.

Use MM that represents Month instead of mm that represents minutes.

使用LocalDate.parse()而不是new LocalDate()来构造LocalDate对象.

Use LocalDate.parse() instead of new LocalDate() to construct the LocalDate object.

DateTimeFormatter format = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd");
LocalDate lDate = org.joda.time.LocalDate.parse("20140518", format);
System.out.println(lDate);

输出:

2014-05-18


org.joda.time. LocalDate#toString()默认使用yyyy-MM-dd模式.


org.joda.time.LocalDate#toString() be default uses yyyy-MM-dd pattern.

您不需要使用todayDate.toString("yyyy-MM-dd").

这篇关于如何在JodaTime中将yyyymmdd格式的字符串转换为LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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