问题
I need to convert a simple String eg:
30.06.2017 18:00
to a Date, but the simpledateformat i set just wont apply. for some reason it gives output like this
Fri Jun 30 18:00:00 UTC 2017
I know there are other questions concerning this issue , but I think iam certain that i followed all the instructions correctly, I set the format correct and the strange thing is , I used the same code in another place and it works fine. I tried using the locale.ENGLISH part and the "strict" attribute.. really seems like netbeans is trolling me
THANK U SO MUCH
System.out.println(newtoday);
gives
"30.06.2017 18:00";
then I try to format it:
try {
thedate = new SimpleDateFormat("dd.MM.yyyy HH:mm").parse(newtoday);
System.out.println(thedate);
} catch (ParseException ex) {
Logger.getLogger(frame1.class.getName()).log(Level.SEVERE, null, ex);
}
Output is
Fri Jun 30 18:00:00 UTC 2017
Thank u dpr for ur answer but the problem is not the "output"
what im doing is :
collect a timeseries for jfreechart:
i have a list of dates to parse and add which works fine
Date thedate=null;
try {
thedate = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.ENGLISH).parse(time);
} catch (ParseException ex) {
Logger.getLogger(frame1.class.getName()).log(Level.SEVERE, null, ex);
}ts1.add(new Millisecond(thedate), ap);
, but the last element of the timesieries has a different format, thats why i have to format it, which works to get a string in the right format (newtoday)
now after i format the date i use again
ts1.add(new Millisecond(thedate), apt);
but it doesnt add to the series
now i thought the problem is the dateformat taht isnt recognized by the addtotimeseries ,
if u say its not the format.. do u see another error?
cheers
回答1:
You output the date using
System.out.println(thedate);
This doesn't use the date format you used to parse the date, but uses the toString()
method of java.util.Date
. This is why you see the date being print in a different format than expected.
To use you date format for parsing and output do something like this:
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date thedate = dateFormat.parse(newtoday);
System.out.println(dateFormat.format(thedate));
来源:https://stackoverflow.com/questions/44849406/java-simpledateformat-returns-wrong-format