Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I have date like mm-dd-yyy in string format and I am trying to convert the same to Date object in java,
Two ways I tried in
sending the date format as "mm-dd-yyy" which is returning the wrong
date, it always returns month Jan even though the month in the
string is not "01"
sending the date format as "MM-dd-yyy" will return the correct date
as expected.
But I want to understand why the first approach returning wrong?
Can any body tell me the reason.
See the Format
M Month in year
m Minute in hour
Date and time formats are specified by pattern strings. Within the pattern strings, m is interpreted as minute in hour, and M is interpreted as Month in year.
Perhaps you should read JavaDoc API spec.
And you should check that the year of parsed date object is what you intended to. Since your string is MM-dd-yyy, "02-12-014"(which should be 2014-02-12 is actually interpreted as 0014-02-12, which can be not your intended result.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
If I have this:
private static final String DATE_FORMAT = "dd/MM/yyyy";
DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
formatter.setLenient(false);
formatter.parse("01/01/98");
Should my application throw an exception if a 2 digit year is passed in? It doesn't seem to have any issue with this.
No. SimpleDateFormat is used for both parsing a Date from a String and generating a String from a Date. The interpretation of the format String varies between these usages. In your case, you are parsing a Date from a String. The 98 is a legitimate value and is interpreted literally (i.e. 98 AD) because you are using yyyy. If you replace the yyyy with yy or y then the parsing should interpret the 98 as 1998. If you want require 4-digit dates then you will need to add some verification code to do that.
Note that setLenient doesn't affect anything here because the values in each position are legitimate. You are not required to have 4 digits in the year position (nor are you limited to 4 digits). If the interpretation is not-lenient and you pass it "1998/04/12" it will throw an exception because 1998 is not in the range of 1-12. If you set lenient then it will mod the value to get it into range (1998 becomes 6) and charge forward. Since the year has no real bounds it has no effect on the year position.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I've noticed that the various Java time parse methods (such as ZonedDateTime.parse(...)) consistently use the relevant portions of 2007-12-03T10:15:30+01:00[Europe/Paris] as the example in their Javadocs (with the exception of Instant which uses UTC as the time zone).
Obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris].
Class
Example
Instant
2007-12-03T10:15:30.00Z
LocalDate
2007-12-03
LocalDateTime
2007-12-03T10:15:30
LocalTime
10:15
MonthDay
--12-03
OffsetDateTime
2007-12-03T10:15:30+01:00
OffsetTime
10:15:30+01:00
Year
2007
YearMonth
2007-12
ZonedDateTime
2007-12-03T10:15:30+01:00[Europe/Paris]
I realize this might just be an arbitrary date, but I've found in the past that oftentime the example values have additional meaning that help my understanding of the overall domain, beyond being just an example.
Is there any particular significance to this datetime, and why was it chosen as the example parse value for the Java time API?
I'm looking specifically for something that can be backed up with something concrete (e.g. official implementation discussions or statements by those involved in the library creation).
No special meaning
No, there is no special meaning to that example date-time value. Date-time handling is tricky enough, do not distract yourself with such trivial detail.
Technical writers commonly work with the same example data across scenarios for consistency, to most easily make apparent the similarities and contrasts.
The value may have personal significance to the original author. But as Arvind Kumar Avinash commented, what matters here is the formats rather than the value.
2007-12-03T10:15:30.00Z is not really an ideal example. I would have chosen a day-of-month larger than 12 to distinguish from the month number. And I would have chosen an hour larger than 12 to make obvious the 24-hour clock (0-23).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to parse timestamps using the format "yyyymmddHHssmm".
I have two such time stamps:
String timeStamp1 = "20190612221303"//this means 12June2019 10:13:03pm
String timeStamp2 = "20190512222303"//this means 12May2019 10:23:03pm
So I am trying to convert these timestamp string to java date using the following :
Date date1= new SimpleDateFormat("yyyymmddHHssmm").parse(timeStamp1);
Date date2 = new SimpleDateFormat("yyyymmddHHssmm").parse(timeStamp2);
So obviously when I do a
System.out.println(date1.getTime() > date2.getTime());
I would expect the above statement to print true.
But alas it prints false.
Inface the .getTime() of Date prints 1547310793000 for date1 and 1547310803000 for date2, which is obviously incorrect.
Could someone point out what is going on here.
In the format string, you have mm twice: yyyymmddHHssmm. The first occurrence should be MM, for month of year.
What is happening is that you are using
m Minute in hour
And your TimeStamp it is parsing with date
Sat Jan 12 22:03:13 Date1
Sat Jan 12 22:03:23 Date2
You need to use
M Month in year
Check more in the documentation
The format that you have used:yyyymmddHHssmm is ambiguous.
I believe the 5th and 6th characters are used to define months.
Use MM in caps for that.
You have used small mm, which means minutes
Your String passed to SimpleDateFormat should be yyyyMMddHHmmss . Take look here which letter stands for which thing in that formatter. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How can I convert the current time in milliseconds, which is a Long, to a date in specific format?
The format that I need is yyyy-MM-dd HH:mm. This should be of type Date, not String.
You are confused. The type Date is a number of milliseconds since January 1 1970 midnight UTC. It has no inherent format. There is a default system format for a Date, but you cannot alter it. You will need to format your Date as a String if you need that particular String format.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can I compare same date of births in GWT using Date?
olpPatient.getBirthday().equals(birthday);
Even if both dates are equal, that line returns false.
You can use the class com.google.gwt.user.datepicker.client.CalendarUtil. There is a method called isSameDate(Date, Date) which will just check the date, not the time of day.
From the javadoc:
Check if two dates represent the same date of the same year, even if they have different times.
It depends on how you create dates. If you get them from a DatePicker or parse a String which does not include hours and minutes, then you can use:
patient.getBirthday().getTime() == birthday.getTime();
If your dates include hours and minutes, you can do:
int day = 24 * 60 * 60 * 1000;
boolean sameDate = patient.getBirthday().getTime()/day == birthday.getTime()/day;
You are comparing Date objects by using equals() (by default, Object.equals is checking whether references to these objects are equal or not and Date.equals checking timestamps). But you need only to exract day, month, year from those objects and compare only them.