We can use date() function alongwith strptime() function to convert string to date object.
To create a datetime object representing a specific date and time, you may use an initialization string: DateTime dt = new DateTime("2004-12-13T21:39:45.618-08:00");
time Description. Provides support for dates, times, time zones, durations, intervals, and partials. This package aims to fully replace the Java Date , Calendar , and TimeZone classes.
Use DateTimeFormat
:
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);
I know this is an old question, but I wanted to add that, as of JodaTime 2.0, you can do this with a one-liner:
DateTime date = DateTime.parse("04/02/2011 20:27:05",
DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss"));
DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss").parseDateTime("04/02/2011 20:27:05");
From comments I picked an answer like and also adding TimeZone:
String dateTime = "2015-07-18T13:32:56.971-0400";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ")
.withLocale(Locale.ROOT)
.withChronology(ISOChronology.getInstanceUTC());
DateTime dt = formatter.parseDateTime(dateTime);
Your format is not the expected ISO format, you should try
DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime time = format.parseDateTime("04/02/2011 20:27:05");
You can also use SimpleDateFormat, as in DateTimeFormat
Date startDate = null;
Date endDate = null;
try {
if (validDateStart!= null) startDate = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH).parse(validDateStart + " " + validDateStartTime);
if (validDateEnd!= null) endDate = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH).parse(validDateEnd + " " + validDateEndTime);
} catch (ParseException e) {
e.printStackTrace();
}
java.time.LocalDateTime.parse(
"04/02/2011 20:27:05" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" )
)
The modern approach uses the java.time classes that supplant the venerable Joda-Time project.
Parse as a LocalDateTime
as your input lacks any indicator of time zone or offset-from-UTC.
String input = "04/02/2011 20:27:05" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
ldt.toString(): 2011-02-04T20:27:05
Tip: Where possible, use the standard ISO 8601 formats when exchanging date-time values as text rather than format seen here. Conveniently, the java.time classes use the standard formats when parsing/generating strings.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With