i have a form where user enters an ad start time and an ad end time, the format of that time is "yyyy/mm/dd hh:mm:ss", then this data is push to a database.
BUT when user enters "yyyy/mm/dd 12:00:00" or basically anything 12, when i use 24 it just doesnt work, the database shows 00:00:00 for when i enter 12. how do i prevent that from happening?
SplashPageValue value = new SplashPageValue();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
value.adStartDate = df.parse(fileUp.getString("adStartDate"));
value.adEndDate = df.parse(fileUp.getString("adEndDate"));
screenshots below:
the form
what i see in database using SQuirreL
Your question is unclear, but I strongly suspect the problem is just that you're parsing using the 12-hour clock. Change your format string to use HH instead of hh, and that may well just fix things:
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
You should also consider which time zone you want to use when parsing, mind you. Personally I prefer to use Joda Time for all date and time operations in Java - including for parsing/formatting, as SimpleDateFormat isn't thread-safe, unlike Joda Time's parsers.
Your date format should be 24 hours:
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Capital H's are 24, lowercase h are 12.
Use HH instead of hh.
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Related
I am baffled why this doesn't work. No matter what I change the day to, it prints out the same thing. What is going on here?
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
String s = "11/22/2000";
Date d = sdf.parse(s);
System.out.println(d.toString());
Output: Sun Dec 26 00:00:00 CST 1999
You're using YYYY which is the week-year instead of the "normal" year. That's usually only used with day-of-week and week-of-year specifiers.
The exact behaviour here would be hard to explain - feasible, but not terribly helpful. Basically the solution is "don't do that". You want a format string of MM/dd/yyyy instead - note the case of the yyyy.
(As a side note, if you can possibly use java.time.* from Java 8 or Joda Time, you'll have a lot better time in Java with date/time issues. It wouldn't affect this particular issue, but it's generally a good idea...)
Use
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy")
instead of
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
So I've been working on a simple little method which would return a String as Calendar object.
Here's what I have:
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
try {
cal.setTime(sdf.parse(this.getCreated_at()));
}
catch (ParseException e) {
e.printStackTrace();
}
return cal;
Now assuming that the String obtained from this.getCreated_at()) is always in the desired format, this should, theoretically, work perfectly.
There's only one little hiccup.
Assume that this.getCreated_at()) returns Mon Feb 10 18:52:54 +0000 2014. Shouldn't be a problem right? The format is correct, and everything.
However when I have my main method do as follows:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
System.out.println(sdf.format(... ... ....stringToCalendar().getTime()));
It gives me the following output:
ma feb 10 19:52:54 +0100 2014
My problem, as you may have noticed, is that firstly, the time is wrong. It's added an hour.
Secondly, it's translated the text fields Mon and Feb into ma and feb which are translations to Norwegian, the place from where I'm coding now.
I need the time returned to me as a Calendar object, in the exact manner which it was obtained from the this.getCreated_at())-string.
What am I doing wrong?
Can anyone help me out?
I'm from Norway, by the way. That's +01:00 Standard Offset.
My problem, as you may have noticed, is that firstly, the time is wrong. It's added an hour.
No it hasn't. Not really. It's displaying the exact same time, but in your local time zone. Unfortunately you can't easily determine the original time zone, because DateFormat.parse returns a Date, which doesn't contain any time zone information. (In this case you don't really know the full time zone anyway - you just know the UTC offset, which isn't quite the same thing.)
EDIT: As noted in Joni's answer, it appears that DateFormat.parse does retain the original information in the format's calendar; you wouldn't want the time zone, but you could use the offset stored in the Calendar object. Personally this seems to me to be an implementation detail which I wouldn't want to rely on (especially as you end up with an offset which isn't necessarily supported by the time zone in the same calendar object!) but you could mangle it if you really wanted.
Secondly, it's translated the text fields Mon and Feb into ma and feb which are translations to Norwegian, the place from where I'm coding now.
That's because your SimpleDateFormat is using your default locale.
You can fix this part very easily:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",
Locale.ENGLISH);
If you know what time zone you want to use, you can specify that on the SimpleDateFormat as well. Otherwise, you're going to have a slightly harder time.
I would recommend that instead, you start using Joda Time. That way you can parse the value as a DateTime, which "knows" which time zone it's in - so you can reformat it however you want, preserving the relevant time zone information.
Sample Joda Time code:
import java.util.*;
import org.joda.time.*;
import org.joda.time.format.*;
class Test {
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormat
.forPattern("EEE MMM dd HH:mm:ss Z yyyy")
.withLocale(Locale.ENGLISH);
String input = "Mon Feb 10 18:52:54 +0000 2014";
DateTime value = format.parseDateTime(input);
System.out.println(format.print(value));
}
}
You can get the Calendar instance from the date format:
sdf.parse(this.getCreated_at());
Calendar parsed = sdf.getCalendar();
This instance has the fields set to what was parsed: UTC timezone, time 18:52:54, etc...
Unfortunately there appears to be no way to format the values held by Calendar using SimpleDateFormat.
I have timestamp string: "1989-01-01 00:00:00" and i need convert it to local date format.
I execute:
SimpleDateFormat TIMESTAMPFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat.getDateFormat(getContext()).format(TIMESTAMPFORMAT.parse("1989-01-01 00:00:00"));
And getDateFormat returns 31.12.1988
Why?
How can I receive 01.01.1989???
In order to skip time-zone when formatting, I would suggest you to set it to default as below:
SimpleDateFormat TIMESTAMPFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TIMESTAMPFORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
This is probably to do with the time zones involved. I strongly suspect that you're formatting in a different time zone to the one you're using for parsing, and the value goes to before midnight, basically. If you can use the same time zone on both, it's likely to work.
If you possibly can, I'd encourage you to use Joda Time instead though - you really want a LocalDate.
In my app, I am using SimpleDateFormat to convert the Date object to a string. But sometime when I change the time zone one by one to test whether the date I enter is the same as the date converted to a string, I found that it shows a different date. For example, suppose I have Thu Mar 15 00:00:00 GMT+08:00 2012 in my Date object, Now when I convert it to a string using SimpleDateFormat it works fine, but when I change the time zone one by one and check whether the date converted to string is same as it stored in Date object then in some cases it shows as 14-Mar-2012 instead of showing 15-Mar-2012. Why this happen? Can anyone please suggest me how to solve this out?
Code I have used:
SimpleDateFormat m_sdFormatter = new SimpleDateFormat("dd-MMM-yyyy");
String selected_date = m_sdFormatter.format(btnSelectedDt.getTime());
try this ,hope it may help you..
private String getDate(long timeStamp) {
DateFormat objFormatter = new SimpleDateFormat("dd-MM-yyyy");
objFormatter.setTimeZone(TimeZone.getDefault());
Calendar objCalendar = Calendar.getInstance(TimeZone.getDefault());
objCalendar.setTimeInMillis(timeStamp * 1000);
String result = objFormatter.format(objCalendar.getTime());
objCalendar.clear();
return result;
}
For example suppose i have Thu Mar 15 00:00:00 GMT+08:00 2012 in my Date object,
You haven't got that (even though that's no doubt what toString displays). A Date object doesn't contain a time zone. It contains an instant in time, which can be interpreted as different dates and times based on the calendar and time zone you use to interpret it. So that's midnight in once specific time zone - but the Date object itself is just a number of milliseconds since the unix epoch.
It's not clear exactly what you're doing, but you shouldn't be surprised that changing the time zone used in SimpleDateFormat will change the date written out. If you can describe in more detail what the larger goal is, we may be able to help you more. Note that if you can use Joda Time instead, that's a much better date/time API - but I know that it's quite large for use in an Android app.
In your SimpleDateFormat, mention the locale,
DateFormat objFormatter = new SimpleDateFormat("dd-MM-yyyy", locale);
where locale is of your choice. For eg, I use Locale.ENGLISH as it contains the date format that I require. Otherwise when you change ure locale in the device, the simpledateformat changes to current locale and you end up getting the wrong date.
I've tried a million different ways of doing this, but with no avail. Any help would be much appreciated.
long millis = getMillisFromServer();
Date date = new Date(millis);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
String formatted = format.format(date);
The above doesn't work.
basically, what I want to do is, get the epoch time and convert it to Australian time. My local time is +05.30 but of course I don't want this to be a factor which contributes to this conversion.
EDIT-
Output when I run your exact code,
epoch 1318388699000
Wed Oct 12 08:34:59 GMT+05:30 2011
12/10/2011 03:04:59
12/10/2011 14:04:59
EDIT: Okay, so you don't want your local time (which isn't Australia) to contribute to the result, but instead the Australian time zone. Your existing code should be absolutely fine then, although Sydney is currently UTC+11, not UTC+10.. Short but complete test app:
import java.util.*;
import java.text.*;
public class Test {
public static void main(String[] args) throws InterruptedException {
Date date = new Date(1318386508000L);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(date);
System.out.println(formatted);
format.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
formatted = format.format(date);
System.out.println(formatted);
}
}
Output:
12/10/2011 02:28:28
12/10/2011 13:28:28
I would also suggest you start using Joda Time which is simply a much nicer date/time API...
EDIT: Note that if your system doesn't know about the Australia/Sydney time zone, it would show UTC. For example, if I change the code about to use TimeZone.getTimeZone("blah/blah") it will show the UTC value twice. I suggest you print TimeZone.getTimeZone("Australia/Sydney").getDisplayName() and see what it says... and check your code for typos too :)
Here’s the modern answer (valid from 2014 and on). The accepted answer was a very fine answer in 2011. These days I recommend no one uses the Date, DateFormat and SimpleDateFormat classes. It all goes more natural with the modern Java date and time API.
To get a date-time object from your millis:
ZonedDateTime dateTime = Instant.ofEpochMilli(millis)
.atZone(ZoneId.of("Australia/Sydney"));
If millis equals 1318388699000L, this gives you 2011-10-12T14:04:59+11:00[Australia/Sydney]. Should the code in some strange way end up on a JVM that doesn’t know Australia/Sydney time zone, you can be sure to be notified through an exception.
If you want the date-time in your string format for presentation:
String formatted = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
Result:
12/10/2011 14:04:59
PS I don’t know what you mean by “The above doesn't work.” On my computer your code in the question too prints 12/10/2011 14:04:59.
Please take care that the epoch time is in second and Date object accepts Long value which is in milliseconds.
Hence you would have to multiply epoch value with 1000 to use it as long value .
Like below :-
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
Long dateLong=Long.parseLong(sdf.format(epoch*1000));