What is wrong with my code here? I'm unable return a String in the format I'd like...
Method Call:
incident.setTargetDate(DateUtil.formatResolutionTime(targetDateList.get(i)));
Formatter:
public class DateUtil {
public static String formatResolutionTime(String startDateString) {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = null;
try {
startDate = df.parse(startDateString);
} catch (ParseException e) {
e.printStackTrace();
}
String newDateString = df.format(startDate);
System.out.println(newDateString);
return newDateString;
}
}
Exception:
java.text.ParseException: Unparseable date: "2017-05-26T00:00:00+02:00"
at java.text.DateFormat.parse(DateFormat.java:366)
at app.util.DateUtil.formatResolutionTime(DateUtil.java:19)
at app.controller.TableViewController.retrieveTicketsForTable(TableViewController.java:194)
at app.controller.TableViewController.initialize(TableViewController.java:139)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at app.Main.start(Main.java:14)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
You need two formats. One for parsing the input format and one for formating the Output.
public static String formatResolutionTime(String startDateString) {
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ"); //Format for parsing the Input string
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); //Format for formatting the output
Date startDate = null;
try {
startDate = dfParse.parse(startDateString);
} catch (ParseException e) {
e.printStackTrace();
}
String newDateString = df.format(startDate);
System.out.println(newDateString);
return newDateString;
}
You are getting the exception Unparseable date: "2017-05-26T00:00:00+02:00"
, because you are trying to parse a String of the format "yyyy-MM-dd'T'hh:mm:ssXXX"
, but telling the formatter that your string is of type MM/dd/yyyy.
Your input String is of the format "2017-05-26T00:00:00+02:00"
and you want the output in the format MM/dd/yyyy
, so as suggested in other posts, you need two formatters.
public static String formatResolutionTime(String startDateString) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssXXX");
Date startDate = null;
try {
startDate = df.parse(startDateString);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat newFormatter = new SimpleDateFormat("MM/dd/yyyy");
String newDateString = newFormatter.format(startDate);
System.out.println(newDateString);
return newDateString;
}
您正在检查格式为MM / DD / YYYY的日期,但提供的日期为YYYY-MM-DD格式。
You have to use two DateFormat objects: one for parsing (input -> date) and one for formatting (date -> output).
Your input is 2017-05-26T00:00:00+02:00
. Let's break it down (refer SimpleDateFormat ):
2017
: the year in the format yyyy
05
: the month in the format MM
26
: the day in the format dd
T
: separator between date and time in the format of character 'T'
00
: the hour in the format HH
( not hh
) 00
: the minutes in the format mm
00
: the seconds in the format ss
+02:00
: the time zone in the format XXX
So your SimpleDateFormat parser pattern will be
yyyy-MM-dd'T'HH:mm:ss:XXX
Additional notes
In case of ParseException your code could result in unexpected behaviour, in fact after the try/catch block your startDate
will be null and the formatter will try to format the null date and return the result, generating a NullPointerException
.
Said that, my code would be:
private static final String PARSE_PATTERN = "yyyy-MM-dd'T'HH:mm:ssXXX";
private static final String FORMAT_PATTERN = "MM/dd/yyyy";
// we cannot declare the SimpleDateFormat as static since it isn't thread-safe
public static String formatResolutionTime(String startDateString) {
try {
DateFormat parser = new SimpleDateFormat(PARSE_PATTERN);
Date startDate = parser.parse(startDateString);
DateFormat formatter = new SimpleDateFormat(FORMAT_PATTERN);
return formatter.format(startDate);
} catch (ParseException e) {
// e.printStackTrace();
return "Error"; // or whatever, but return a string here
}
}
This thread of answers would not be complete without the modern answer.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
String newDateString = OffsetDateTime.parse(startDateString).format(dtf);
The result is
05/26/2017
The answers I have read until now are correct — and outdated. In 2017 it's time to skip the old classes SimpleDateFormat
and Date
, and use the newer Java date and time API , in this case DateTimeFormatter
and OffsetDateTime
. This is even more appropriate as your original date-time string conforms with ISO 8601, the format that the newer classes “understand” as their default. So we need not give an explicit format for parsing (as would be needed with SimpleDateFormat
).
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
//System.out.println(dateFormat.format(date));
return dateFormat.format(date);
It works for me)
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.