is there any function like "strptime" in java
suppose there is a time_string like: "wallclock(2011-09-22T01:52:00)"
in C language, I can use "strptime(time_string, "wallclock(%Y-%m-%dT%H:%M:%S)", &tm)
"
to get date time and store 开发者_JS百科it into the struct tm,
and then use mktime(&tm)
to get the date in seconds
But in Java, I find no way about how to transfer string "wallclock(2011-09-22T01:52:00)"
,
is there any way to do this job? thank you :-)
You can either use SimpleDateFormat
:
DateFormat format = new SimpleDateFormat("'wallclock('yyyy-MM-dd'T'HH:mm:ss')'");
Date parsed = format.parse(text);
(Note that you probably want to set the time zone of format
appropriately.)
Or preferrably (IMO) use Joda Time to do the parsing:
String pattern = "'wallclock('yyyy-MM-dd'T'HH:mm:ss')'";
DateTimeFormatter formatter = DateTimeFormatter.forPattern(pattern);
LocalDateTime localDateTime = formatter.parseLocalDateTime(text);
This will parse to a LocalDateTime
which doesn't have a time zone.
In Java 8, you'd use java.time.format.DateTimeFormatter
instead.
SimpleDateFormat
will get you pretty close.
use SimpleDateFormat
class. It provides the functionality you need.
精彩评论