I'm trying to create a 'fixed' time (midnight in 24 hour clock format, i.e., 00:00:00) to set as a string for a SQL SELECT query using the the following...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar todayDate = new GregorianCalendar();
Log.d(TAG, "todayDate: " + todayDate.getTime().toString());
Log.d(TAG, "formatted todayDate: " + sdf.format(todayDate.getTime()));
todayDate.clear(Calendar.HOUR);
todayDate.clear(Calendar.MINUTE);
todayDate.clear(Calendar.SECOND);
todayDate.set(Calendar.HOUR, 0);
todayDate.set(Calendar.MINUTE, 0);
todayDate.set(Calendar.SECOND, 0);
Log.d(TAG, "formatted modified todayDate: " + sdf.format(todayDate.getTime()));
This is fine UNLESS the current time is PM. For example,
todayDate: Fri Jan 28 23:34:34 GMT 2011
formatted todayDate: 2011-01-28 23:34:34
formatted modified todayDate: 2011-01-28 12:00:00 <- THE hour is 12 not 00
If I do this when the current time is between midnight and midday, (i.e., 00:00:00 -> 11:59:59 AM) then my hour in the formatted string is correctly set to 00. If I do it at any time after midday and before midnight then I get 12 for my hour and not 00.
Can anybody explain this and help me find a fix (or alternative way of doing things) please?
You need to set HOUR_OF_DAY to 0 instead of HOUR
todayDate.set(Calendar.HOUR_OF_DAY, 0);
From the API docs:
HOUR Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.
HOUR_OF_DAY Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
Related
I'am using GregorianCalendar class to manipulate with date and time.
I need to get only a current date without time.
My code:
Calendar today = new GregorianCalendar();
today.set(GregorianCalendar.HOUR,0);
today.set(GregorianCalendar.MINUTE,0);
today.set(GregorianCalendar.SECOND,0);
today.set(GregorianCalendar.MILLISECOND,0);
Date todayDate = new Date();
todayDate.setTime(today.getTime().getTime());
I expect todayDate will be like this "Wed Dec 07 00:00:00 EET 2016"
But actually todayDate is "Wed Dec 07 12:00:00 EET 2016".
Which is the correct way to do it?
Ia understend difference between fields HOUR and HOUR_OF_DAY when I get value, but why when I set value of HOUR to "0" the HOUR_OF_DAY is not seting to "0" automatically. Zero is always zero...
Question is about Data integrity...
It is a mistake to thought zero is always zero... It is not true for hours after midday. Zero hours after midday in short form (am/pm) is 12 hours in 24 form.
Thanks to #selvin
Here, is my code to convert ISO to IST and ISO to GMT.
Log.e("ISO TIME",""+time);//The time which i got from JSON file.
//IST TIME ZONE
Date date=new Date();
DateFormat format=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat();
simpleDateFormat.applyPattern("yyyy-MM-dd'T'HH:mm'+00':ss");
date = simpleDateFormat.parse(time.toString());
format.setTimeZone(simpleDateFormat.getTimeZone());
Log.e("IST TIME", "" + format.format(date));
//GMT TIME ZONE
Date date1=new Date();
SimpleDateFormat sdf=(SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
sdf.applyPattern("yyyy-MM-dd'T'HH:mm'+00':ss");
date1=sdf.parse(time);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.e("GMT TIME", "" + sdf.format(date1));
Here, is my output
E/ISO TIME: 2016-01-18T08:40+00:00
E/GMT TIME: 2016-01-18T03:10+00:00
E/IST TIME: Jan 18, 2016 8:40:00 AM India Standard Time
Problem is the actual difference between IST and GMT is 5:30
But in my output i was got the difference exactly 5:30
Please, help me to solve out this problem.
#Logic was definitely right.
But i have some suggestion for your.
You need to add 5:30 hours into your GMT time then you will get IST.
Never add you 5:30 hours into your IST time.
Look at this example
your IST time is 8:40 and GMT time is 3:10
1 hour added each operation
In round brackets one hour add for every iteration:
3:10->4:10(1)->5:10(2)->6:10(3)->7:10(4)->8:10(5)
Remaining IST time minutes to complete an hour is 00:20
add 00:20 into 8:10 it will become a 8:30.
take GMT time for addition
3:20
Your code is absolutely right
I know this may be a simple/duplicate question but somehow I can't manage to calculate it
Hello, I am fetching today's twitter feeds with their tweeting times and have to calculate the difference of hours between them based on location to show. This is a sample date Sun Jul 21 21:14:40 +0000 2013 and I need to calculate the difference of hours with current time.
So, the first thing to do is to parse that string into a date:
Date postDate = new java.text.SimpleDateFormat("E MMM d HH:mm:ss Z yyyy")
.parse("Sun Jul 21 21:14:40 +0000 2013");
Now, you can get the difference between the post date and the current date in milliseconds:
long diffMSec = new Date().getTime() - postDate.getTime();
Finally, you can convert the milliseconds to hours, or any other time unit:
int diffHours = diffMSec / (1000*60*60);
Note that dates only have time zones when represented as strings - internally they're stored as milliseconds since midnight, Jan. 1, 1980, UTC. So parsing the string to a date (using a time zone), and calculating the diff between two dates takes care of the timezone difference for you.
update: I thought the Android version was to blame, but it turns out it is the user-timezone
This code produces incorrect output when my tablets time is in Central European time (+2 in summer time):
SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss Z", Locale.GERMAN);
Date testDate = dateTimeFormatter
.parse("2999-01-01 00:00:00 +0100");
Log.v(TAG, "test 1 " + testDate);
testDate = dateTimeFormatter.parse("2099-01-01 00:00:00 +0100");
Log.v(TAG, "test 2 " + testDate);
"test 1 Mon Dec 31 23:19:32 CET 2998"
"test 2 Thu Jan 01 00:00:00 CET 2099"
There is a time difference. Why the 40 minutes and some seconds difference on the larger date?
The bug is not present when I put my tablet in (most) other timezones. Something to do with timezones that have dailight saving hours?
Guess I cannot overcome this bug, I've built my own date parser.
Update: my own parser has the same problem
My current solution is just not using these large dates. If date > 100 years in future, I set it to 100 years in the future.
I am using Calendar Class in my android app to calulate day of year of a date and then do some comparison with current day of year. Here is the code I use :
Date now=new Date();
Calendar ca1 = Calendar.getInstance();
ca1.set(now.getYear(), now.getMonth(), now.getDate());
int nowC=ca1.get(Calendar.DAY_OF_YEAR);
//Date arg0=say,get from user
Calendar ca2 = Calendar.getInstance();
ca2.set(arg0.GetBirthDay().getYear(),arg0.GetBirthDay().getMonth(),arg0.GetBirthDay().getDate());
int d1C=ca2.get(Calendar.DAY_OF_YEAR);
I debug my application and I see the following value for current day :
Fri Mar 02 14:18:33 Asia/Tehran 2012
and for arg0:
Fri Mar 02 00:00:00 Asia/Tehran 1979
And 'nowC' got 62, and 'd1C' got 61.
I expect them to be equal cause both of them has same month and day, also If I use DateTime class of joda package, as below, I get the same results:
int ndy=dtnow.getDayOfYear();
int d1dy=dt1.getDayOfYear();
Why it is happening ?
2012 was a leap year, 1979 was not. There is an extra day before March 2nd this year - so both APIs are giving you the right answer!
I suspect you want to compare both the month and day-of-month, to get the semantics you are expecting.