This question already has answers here:
Java date parsing with microsecond or nanosecond accuracy
(3 answers)
Closed 4 years ago.
When I use this code to parse a date String, the time is parsed incorrectly. This code prints Wed Feb 14 15:06:06 EST 2018. What date format should I use to parse this input?
String input = "2018-02-14 14:57:59.487927";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
System.out.println(formatter.parse(input));
So far with my testing the best apporach for this is using DateTimeFormatter that will math what you want.
String input = "2018-02-14 14:57:59.487927";
DateTimeFormatter r =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
LocalDateTime a =LocalDateTime.parse(input,r);
System.out.println(a);
With this you will get
Hour: 14
Minite: 57
Second: 59
Nano: 487927000
And with SimpleDateFormat you will get milisecond always with 927 and so far you will always loose precision.
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
So far you best precission is only 3 digits and there is no nanoseconds in SimpleDateFormat.
I suggest to use the new Java 8 Date libraries.
Related
This question already has answers here:
How to add one day to a date? [duplicate]
(18 answers)
Closed 5 years ago.
I have the following Java code that takes a date and should add a full day to the date:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = "2017-01-30T19:00:00+0000"
Date date = formatter.parse(dateString);
long timeBetweenStartDates = 24 * 60 * 1000;
Long DateWithOneDayAddedInMilis = date.getTime()+timeBetweenStartDates;
Date dateWithOneDayAdded = new Date((DateWithOneDayAddedInMilis));
The value I am getting for dateWithOneDayAdded is:
Mon Jan 30 13:24:00 GMT 2017
What I am looking for here would be:
Tue Jan 31 13:24:00 GMT 2017
How can I ensure that the date is in the format I expect?
Oh, what a wonderful example of where the newer date and time classes are much more programmer-friendly. With these it’s next to impossible to make an error like the one you made (and which is pretty easy to make with the old classes, and in particular, very hard to spot once you have written the code).
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
String dateString = "2017-01-30T19:00:00+0000";
OffsetDateTime dateTime = OffsetDateTime.parse(dateString, formatter);
OffsetDateTime dateWithOneDayAdded = dateTime.plusDays(1);
System.out.println(dateWithOneDayAdded);
This prints
2017-01-31T19:00Z
You may of course format the calculated date-time the way you or your users prefer.
An added benefit is that plusDays() handles transistion to and from summer time (DST) nicely and hits the same time on the next day (if possible) rather than blindly adding 24 hours.
This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 6 years ago.
The code I've got should convert the date of birth input to DD/MM/YYYY format, which it does but for example when I input 20/08/2000 it sees the date as 3rd January.
System.out.println(this.dob);
DateFormat dateF = new SimpleDateFormat("dd/MM/YYYY");
Date birth = dateF.parse(this.dob);
System.out.println(birth);
Which outputs
20/08/2000
Mon Jan 03 00:00:00 GMT 2000
Using capital Ys in your format means something called the "week year".
Instead, use lowercase ys in your format, which means the year as you'd expect.
new SimpleDateFormat("dd/MM/yyyy");
This question already has answers here:
How to convert a date String to a Date or Calendar object?
(5 answers)
Closed 6 years ago.
As I am not expert in handling of dates in java but I am unable to understand this behaviour.Here is my code
Date from = new SimpleDateFormat("dd/MM/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output
Sat Jul 05 00:07:00 PKT 2013
And this is 2nd another code snippet
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
System.out.println(from);
which gave me this output:
Sat Jan 05 00:07:00 PKT 2013
Now the thing which is considerable is format. This format dd/MM/yyyy which have MM gave me correct output but this format dd/mm/yyyy which have small mm gave me wrong output (always give jan in month).I read the doc where it is mentioned that samll m is for minutes and capital M is for month My question is Can I never use small m here? if no , then why it is giving the result and on which basis it is giving jan everytime I know this is a basic question but after searching and after not finding any understandable thing , I posted it.Thanks
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
that mm in your format is for minutes. MM is for month.
Those formatting placeholders are fixed. small m is always for minutes. And it's January because this is the default Month value.
mm is for minutes so you do not have any month in your date. Thus, I guess that the month is initialized to 0 (Jan)
The reason it does not fail is because by default the formatter is lenient. If you want it to fail then setLenient(false) on the formatter object.
Although I do not think it will fail in your case as in your example it will read 07 as minutes.
When using
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
the simple mm is for minutes therefore i think the value for month is assumed to be 0 so it gives Jan as the default value so use MM for month.
I guess it comes from calendar.clear() which will point to 1 Jan 1970. Then it adds you parsed data = 2013 years (YY), 5 days (dd) and 7 minutes (mm). Use MM for month
This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
getting the difference between date in days in java [duplicate]
(3 answers)
Closed 10 years ago.
I am co-working with a group, I want to ask how can i get the date difference from a formate of "Sat Feb 23 00:00:00 GMT 2013". The pickerfrom and to is a calendar, and getDate returns that formate. How can I get the date difference in days? any idea?
/* Current format Sat Feb 23 00:00:00 GMT 2013 */
Date date_from = pickerFrom.getDate();
Date date_to = pickerTo.getDate();
int date_diff = (int)((date_to)-(date_from));
Checkout getting the difference between date in days in java
My preference would be to use Joda time - it has many useful date functions that'll make your life much easier when it comes to dates and date manipulation
You can get the difference in milliseconds of each date and subtract these values.
long diff = date_to.getTime() - date_from.getTime();
will return you the number of milliseconds between the two dates.
Then, you can use something like this to get the number of hours, days,... out of these milliseconds.
I have a requirement in project
to Convert 24 hours format to 12 hours format
for example
if it is 17:12:01 it should be converted to 05:12:01
You can use a SimpleDateFormat http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html to format it.
Date date = new Date(yourdate);
// format however you see fit
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
String formatted = format.format(date);
As suggested by Software Monkey, also use a SimpleDateFormat to parse your 24hr date if you don't already have it in millis.
Use two SimpleDateFormat objects - one to parse and the other to format the time.
Note also that 05:12:01 is not technically a 12 hour time (rather it appears to be an AM 24 hour time, given the lead 0 on the hour and the lack of a meridian designation). You probably want 5:12:01 PM.
It is possible that a 12 hour SimpleDateFormat will correctly parse a 24 hour time, provided that it is configured for lenient parsing. Just saying, so perhaps you need only one SimpleDateFormat.