I'm trying to put a user provided date into an SQL database, and I have the following lines to process the string and convert it to a java.sql.Timestamp object.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm");
java.util.Date badDate = formatter.parse(request.getParameter("datetime"));
Timestamp date = new Timestamp(badDate.getTime());
The issue is, badDate is the correct date that the user input, but date always gets set to the wrong month and day, usually January 2, and the correct time and year. I had the same problem when I tried to convert to a java.sql.Date object, but then the time was set to midnight as well. I couldn't find anyone with a similar problem after some searching, maybe someone here has seen something like this?
The date format is wrong, it should be yyyy-MM-dd'T'HH:mm.
Please refer this document for details about date format.
m stands for Minutes
M for Months
h for hours in am/pm (1-12)
H for hours in 24 hour clock
It will be better if you can give a sample of the date value you are trying to parse.
You want "yyyy-MM-dd'T'hh:mm" (note capitalized MM). Otherwise your month is wrong in the format string.
Related
I have this simple code:
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm yyyy-MM-dd");
DateTime dateTime = formatter.withZone(DateTimeZone.forID("America/New_York")).parseDateTime("08:30 2015-06-01");
DateTime dateTime2 = formatter.withZone(DateTimeZone.forID("America/New_York")).parseDateTime("08:30 2015-12-01");
these are leap times. when I hit toString method, I got something like this:
2015-06-01T08:30:00.000-04:00
2015-12-01T08:30:00.000-05:00
which is correct, we can see UTC time - offset. But when I call getHourOfDay, I got 8 and not 4/3 as expected. What am I doing wrong? Please, share some advices here.
Well, from the Javadoc for DateTimeFormatter#withZone():
Returns a new formatter that will use the specified zone in preference to the zone of the printed object, or default zone on a parse.
So, you told the formatter to use the specific timezone on parsing AND output, and the input you gave it did NOT contain a timezone, so this is the expected result. In essence you said:
Here's a date string without timezone, parse it assuming America/New_York
Convert the date back to String, in the timezone America/New_York
This is what it did.
I have tried all the ways in all the other questions on SO, and I can't get it to work. It is making me want to kill myself.
I have a set of times which are something like "04:00 AM AEST", except the AEST is a glitch, they should be GMT. What I want to do is change them to "04:00 GMT", and then convert them up to the correct AEST times (which in this example would be "14:00 AEST"). I have tried everything, and nothing works. The closest was to manually make a new DateTime using each individual value from the original date, e.g.
DateTime dt = new DateTime(origdate.year, origdate.month, origdate.day, origdate.hour, origdate.minute, origdate.second, timezone.GMT)
But for some reason the results came out four and a half minutes over, which is weird because timezones differ on hours and half hours.
1st Method By following lines you will get GMT time in specified format :
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("dd-MM-yyy HH:mm:ss z");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = date.format(currentLocalTime);
Hence, from GMT you can derive the time of any place.
2nd Method You can get system time of current place in milliseconds by:
Long current_time = System.currentTimeMillis() / 1000L;
Hope it helps.
Please have a look at the below code
SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
Date parse = format.parse("05-10-2014");
java.sql.Date sqlDate = new java.sql.Date(parse.getTime());
The output "should be" 05-10-2014, but I get the output as 2014-01-05. What is wrong here?
Even when I save the sqlDate in database, it is still being saved as 2014-01-05.
UPDATE
I changed the dd-mm-yyyy to dd-MM-yyyy. Now, mysql saves it as 2014-05-10
If you're worried about the formatting of your dates, the java.sql.Date toString() method, the one that's called when you println it, always formats the date to be yyyy-mm-dd.
If you want to format your dates in a more controlled manner, you can use DateFormat when outputting it.
However, the real problem here (bad data) is that you're using lower-case m which is the format character for "minute in hour", not "month in year". See the SimpleDateFormat page for detail:
Letter Date or Time Component Presentation Examples
------ ---------------------- ------------ --------
M Month in year Month July; Jul; 07
m Minute in hour Number 30
In that second link above, the description for pards() has this little snippet:
This parsing operation uses the calendar to produce a Date. All of the calendar's date-time fields are cleared before parsing, and the calendar's default values of the date-time fields are used for any missing date-time information.
Since the default value for Calendar is January 1, 1970, and you're only setting the day, minute and year, you're ending up with January 5, 2014 (ten minutes past midnight), assuming UTC.
Changing the format string to "dd-MM-yyyy" should fix the immediate problem.
I get an Date with format: MM-dd-yy hhmma zzz. The problem is that i only get an A for AM or P for PM. How can I parse a string with that format to a date object?
Alex
Use this 'a'
example: MM-dd-yy hhmma a;
Visit http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
This pattern is working for me:
dd-MM-yyyy hh:mm:ss a
It prints the information in this format:
12-02-2016 03:50:07 PM
Simple, do a string replace of A->AM/P->PM before parsing. SimpleDateFormat does not support A/P for AM/PM.
dd-MM-yyyy (MM-stands for month). Please do not use MM in the time section of the DATE TIME hh:MM:ss.
For example. It will display the date '15-02-2017 12:12:05' as
'15-02-2017 12:02:05' instead of 12:12:05. Month replaced in the place of minutes.
hh - stands for 12 hours time format
HH - stands for 24 hours time format
it is best to use 12 hours time format with a 'hh:mm a' to give clear picture on whether it is 'AM/PM'
Currently I have a timestamp for example say, 2012-06-23 14:24:07.975 and
two times - 8AM and 3PM.
In java, how can I check whether the above timestamp is between the particular two time.
In other words, I need to check whether the time in timestamp (2012-06-23 14:24:07.975) falls between 8AM and 3PM or not.
Any suggestions.
You can convert the timestamp into an actual Date afterwards you convert it into a Calendar from where you can extract the hour and check it against your given hours.
Use SimpleDateFormat for getting a Date out of your Timestamp, if you cannot directly convert it.. Afterwards
Calendar cal = new GregorianCalendar();
cal.setTime(yourDate);
int hour = cal.get(Calendar.HOUR_OF_DAY);
Now do your hour check.
before and after methods should help you to do that. See docs.
I suggest using compareTo method in Date.