parseexact java中parse什么意思包 java

datetime - Parse Date String to Some Java Object - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
J it only takes a minute:
I am working in a project that reads files and processes data. There I got to work with dates for example:
January 13, 2012
I found the package Joda, kinda interesting package but don't know if it is the easiest around.
I was able to parse the first example to a DateTime object (Joda) reg-ex and String manipulation. (Ex: by replacing the space by '-' and passing it to constructor.
new DateTime(" 23:13:26".replace(' ', '-'))
I guess it worked, but the problem is with the second format. How can I use such an input to extract a an object, preferably a Joda object. I sure can write a function to change the format to what Joda supports, but was wondering if there would be some other way (even some native Java library) to do it.
If there are any thing better than Joda out there, please let me know it as well.
Thank you.
39.3k42184248
1,01942347
Using Joda-Time, it allows parsing both kind of date strings that you mention (and almost any other arbitrary formats). If your needs are even more complex, try .
To parse #1:
DateTimeFormatter f = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = f.parseDateTime(" 23:13:26");
Edit: actually
is a more appropriate type for a datetime without a time zone:
LocalDateTime dateTime = f.parseLocalDateTime(" 23:13:26");
And for #2:
DateTimeFormatter f = DateTimeFormat.forPattern("MMMM dd, yyyy");
LocalDate localDate = f.parseLocalDate("January 13, 2012");
And yes, Joda-Time is definitely the way to go, as far as Java date & time handling is concerned. :)
As mostly everyone will agree, Joda is an exceptionally user-friendly library. For example, I had never done this kind of parsing with Joda before, but it took me just a few minutes to figure it out from the API and write it.
Update (2015)
If you're on Java 8, in most cases you should simply use
instead of Joda-Time. It contains pretty much all the good stuff—or their equivalents—from Joda. For those already familiar with Joda APIs, Stephen Colebourne's
comes in handy.
Here are java.time versions of above examples.
To parse #1:
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.from(f.parse(" 23:13:26"));
You cannot parse this into ZonedDateTime or OffsetDateTime (which are counterparts of Joda's DateTime, used in my original answer), but that kinda makes sense because there's no time zone information in the parsed string.
To parse #2:
DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
LocalDate localDate = LocalDate.from(f.parse("January 13, 2012"));
Here LocalDate is the most appropriate type to parse into (just like with Joda-Time).
39.3k42184248
SimpleDateFormat will parse dates into Java Date objects:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // first example
SimpleDateFormat format2 = new SimpleDateFormat("MMMMM dd,yyyy"); // second example
Date d1 = format1.parse( dateStr1 );
Date d2 = format2.parse( dateStr2 );
21.8k1774107
I would imagine Joda has something of a Formatter to do this for you.
I found this with a quick google search:
DateTimeFormatter parser1 =
DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss Z");
DateTimeFormatter parser2 =
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
DateTime time = parser1.parseDateTime("&data&");
The syntax that is used to evaluate the patterns can be found in .
JodaTime is largely considered the de-facto standard for date-time processing in Java - they're working to get it added to the next version of the Java library (well, effectively).
For getting JodaTime dates from strings, you're going to want to look into the
8,60031733
Easiest would be setting up SimpleDateFormat properly as per the format you would expect and use its parse method to give you a Date object
LocalDateTime ldt = LocalDateTime.parse( " 23:13:26".replace( " " , "T" ) );
LocalDate localDate = LocalDate.parse ( "January 13, 2012" , DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG ).withLocale ( Locale.US ) );
is basically correct but misses some important issues.
If there are any thing better than Joda out there, please let me know it as well.
Yes, there is something better, the
framework. The Joda-Time team advises migration to java.time as its successor.
framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.
To learn more, see the . And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in
and further adapted to Android in .
Your first input is a date plus a time-of-day. It lacks any
info, so we parse it as a
object. The “Local…” means any locality, no specific locality. So it is not an actual moment on the timeline but rather just a rough idea about a moment.
To parse the input string
23:13:26 we can replace the SPACE in the middle with a T to conform with the canonical style for the
standard format of date-time values.
The java.time classes use ISO 8601 formats by default when parsing and generating textual representations of date-time values. So no need to define a parsing pattern.
String input = " 23:13:26".replace( " " , "T" );
LocalDateTime ldt = LocalDateTime.parse( input );
If you know the the intended time zone from the context of this value, apply it to define an actual moment on the timeline.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( zoneId );
Dump to console.
System.out.println ( "input: " + input + " | zdt: " + zdt );
input: T23:13:26 | zdt: T23:13:26-05:00[America/Montreal]
The second of you inputs is a date-only value without time-of-day and without time zone. For that we need the
I recognize the format of that input as complying with the language (English) and cultural norms of the United States. So no need to specify explicitly a formatting pattern. We can simply ask for a formatter that knows that US format by specifying a . We specify
as appropriate for the length of this format.
String input = "January 13, 2012";
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG ).withLocale ( Locale.US );
LocalDate localDate = LocalDate.parse ( input , formatter );
Dump to console.
System.out.println ( "input: " + input + " | localDate: " + localDate );
input: January 13, 2012 | localDate:
44.3k9146199
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .24681
Stack Overflow works best with JavaScript enabled1317人阅读
我做了一个测试,
now = ,这样格式化字符串使用“yyyy/M/d”是没有问题的,但有一个问题,今天如果不是7号,也不是3月呢?
如果是10月,是17号呢?
经过测试,原来这个是有兼容性的。格式化字符串使用“yyyy/M/d”依然可用,不需要改为“yyyy/MM/dd”,这样的解决问题就简单多了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:5010682次
积分:45711
积分:45711
排名:第54名
原创:670篇
转载:191篇
译文:21篇
评论:1171条
文章:129篇
阅读:1297668
文章:33篇
阅读:158063
文章:16篇
阅读:110468
阅读:8565
文章:29篇
阅读:263111
文章:18篇
阅读:216211
文章:44篇
阅读:287904
(21)(20)(17)(4)(16)(12)(17)(25)(34)(19)(15)(17)(15)(23)(12)(12)(14)(13)(14)(20)(58)(25)(31)(16)(33)(31)(20)(24)(23)(21)(20)(14)(36)(14)(14)(7)(6)(15)(3)(2)(9)(6)(7)(32)(3)(8)(7)(22)(2)(30)(4).NET基础语法(82)
c#中关于时间转换提供了两种方法:DateTime.ParseExact()和 Convert.ToDateTime(string),ParseExact使用起来会更加灵活,可以由用户自己来定义时间格式,比如数据库里某字段的值都是这种形式,我们可以这样转换:
DateTime time1 = DateTime.ParseExact(dt,&yyyyMMdd&, null);
dt为这样的字符串:&
来自:-/shtml/article/net/csharp//421.shtml
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:973367次
积分:12854
积分:12854
排名:第790名
原创:310篇
转载:267篇
评论:230条
(3)(4)(1)(3)(7)(6)(6)(6)(1)(1)(1)(1)(1)(3)(1)(1)(2)(2)(2)(2)(6)(5)(2)(1)(2)(4)(8)(3)(15)(13)(4)(1)(1)(1)(1)(2)(7)(5)(26)(111)(41)(39)(9)(7)(1)(2)(9)(3)(3)(1)(2)(52)(23)(3)(14)(16)(26)(2)(7)(1)(8)(1)(1)(12)(23)(1)
() () () ()
() () () () ()}

我要回帖

更多关于 java date parse 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信