Using the below code I'm attempting to convert a time: "2014-10-31T23:59:59"
to just "yyyy-MM-dd"
format, so in this case "2014-10-31"
.
Here is the code :
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class TestConvert {
public static void main(String args[]){
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime dt = formatter.parseDateTime("2014-10-31T23:59:59");
System.out.println("formatted date is "+dt.toString());
}
}
exception :
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014-10-31T23:59:59" is malformed at "T23:59:59"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899)
at (TestConvert.java:11)
How to ignore the time and just create a date?
You need a different formatter to parse the date in its original format:
public static void main(String args[]){
DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dt = parser.parseDateTime("2014-10-31T23:59:59");
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
System.out.println("formatted date is " + formatter.print(dt));
}
Try this:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
DateTime dt = formatter.parseDateTime("2014-10-31T23:59:59");
System.out.println("formatted date is "+dt.toLocalDate());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.