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
Related
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.
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:
last day of month calculation
(10 answers)
Closed 6 years ago.
I have a given date:
01/10/2017(mm/dd/yyyy)
Calendar c = c.getInstance();
c.setTime(date);
Now to point to the last day of the month I am using the following code:
c.set(Calendar.Date, c.getActualMaximum(Calendar.Date));
Expected Output : 01/31/2017
Original Output : 02/01/2017
I am not getting the expected output. Its returning me the first day of next month.
Can anyone please help me?
You better use the new date time features of Java 8 here:
LocalDate date = LocalDate.of(2000, Month.OCTOBER, 15);
LocalDate lastOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
System.out.printf("last day of Month: %s%n", lastOfMonth );
Yes, you could theoretically also use Calendar objects and do all kinds of low level operations yourself. But chances are: you will get that wrong ... well, unless you look here and follow the advise from there.
But as Basil is correctly pointing out: The 310 project is pretty close to java.time (because the later was designed to match the former); and 310 can be "ported" back to Java7. So if there is more than one place where you need to deal with dates, I would look into exactly that: making your life easier by using the "sane" date/time library.
I have doubts about mm/dd/yyyy. Try this
Date d = new SimpleDateFormat("MM/dd/yyyy").parse("01/10/2017");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
System.out.println(c.getTime());
it gives expected date:
Tue Jan 31 00:00:00 EET 2017
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 8 years ago.
I'm processing a list of dates from an input file, and I need to convert each from String to Date. Examples of the format:
9/2/2013 7:34:17 PM
1/13/2011 10:47:36 AM
Each time a line is read, the date is stored in the String variable dateAsString. Here's what I've got:
DateFormat format = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");
Date myDate = format.parse(dateAsString);
System.out.println(myDate.toString());
The output is incorrect:
9/2/2013 7:34:17 PM becomes Sun Dec 30 19:34:17 EST 2012
1/13/2011 10:47:36 AM becomes Sun Dec 26 10:47:36 EST 2010
It seems pretty straightforward, so I'm confused. What am I doing wrong?
Just try 'yyyy' instead of 'YYYY'
Capital 'Y' means a "week year" where 'y' represents the actual year. Try using yyyy instead.
For more formatting help, check out the SimpleDateFormat API where they give you examples of the patterns to use.
Extra Info
Just in case you're wondering, a "week year" is a year where all the weeks in the year are whole weeks
Your format is not correct, as you can see in Javadoc :
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use yyyy instead of YYYY for the year part
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.