Joda Time hour interval - android

I get time from server in format { "from" : "12-00", "to" : "01-00" }
Then, I parse it like this
DateTimeFormat.forPattern("HH:mm").parseDateTime(dateString)
Then, I need to check if current time is in this hour range:
Interval(from, to).containsNow()
Interval constructor throws error because second date more than first
DateTime object time is 1970-01-01T12:00:00.000Z and 1970-01-01T01:00:00.000Z
How can I check if current hour is in hour range? Is there any hour range in Joda?

Since you are using 24-hour format, you need to use the date as well. You are currently comparing 12:00 of 01-01-1970 (the Unix start Date) with 01:00 of the same day, and 12:00 is after 1:00 and that is an invalid interval.
Change the DateTime pattern to "MM/dd/yyyy HH:mm" or something else that works for you and contains both the date and the time that suits your needs. Then, you can make the check.

Related

How to get 2 date objects holding the current time, one in GMT and the other in local

I am trying to fill 2 date objects, one in Local time and the other in UTC.
I AM NOT TRYING TO PRINT THE DATE AS A STRING IN GMT/UTC, please do not suggest DateFormatting, and dont say its a duplicate until you read the full question.
Local, I have no problem:
Date dateLocal = new Date();
The problem is I cant get the utcDate to be UTC.
Using a Calendar like so:
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.setTimeZone(TimeZone.getTimeZone(utcTimeZone.getID()));
Date utcDate = c.getTime();
When debugged or submitted to the webservice, utcDate shows in my local timezone, instead of UTC.
Using Joda:
DateTime utcDateTime = DateTime.now(DateTimeZone.UTC);
Date utcDate = utcDateTime.toDate();
Same issue, utcDate when debugged/submitted to webservice is showing in local time.
Here is how the object looks when debugged:
This is an issue because this causes the webservice (which i have no access to) to think this time is UTC, so when it does its work and conversions, the time is always off by 4 hours, since for me the UTC to Local conversion is GMT -4.
The ONLY way i have been able to get this to submit the date in UTC time is by adding:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
BUT this also changes the LocalTime object, even though this object was defined and set before the default TimeZone was changed.
So i get it, the Date() object uses the JVM locale, so any time a Date is created, its created in the default timezone, and apparently whenever the default timezone is changed, all of the Date objects (even if they are already created) change to the new default timezone... I know Date objects are just the millis between now and 1970 whatever, but the TimeZone is obviously being taken into account in the Webservice and this is messing up my results...how can i get the dates the way i want?

JodaTime confused about TimeZone offset

I receive a Date String like "2018-06-21T13:30:00Z"
I parse it with the pattern "yyyy-MM-dd'T'HH:mm:ssZ"
I am in GMT+2 and the result looks like 2018-06-21T15:30:00.000+02:00
while i expected it to look like this 2018-06-21T13:30:00.000+02:00
Is the offset supposed to be already applied in the HH:mm:ss part of the Result?
Code
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").parseDateTime("2018-06-21T13:30:00Z")
joda:2.9.7
Yes the date time given is in the local time of +02:00.
From ISO 8601 Wikipedia Time offsets from UTC:
The following times all refer to the same moment: "18:30Z", "22:30+04", "1130−0700", and "15:00−03:30". Nautical time zone letters are not used with the exception of Z. To calculate UTC time one has to subtract the offset from the local time, e.g. for "15:00−03:30" do 15:00 − (−03:30) to get 18:30 UTC.
So for your case: 2018-06-21T15:30:00.000+02:00 means 15:30 - 02:00 so a UTC of 13:30

Getting Arizona time with getTime()

date= Calendar.getInstance();
Date currentDate = date.getTime();
String sDate = currentDate.toString();
This returns time EST. I need to change it to Arizona time which is tricky because Arizona does not have daylight savings time. Is there a short cut to making the changes or do I need to query a calendar to subtract two hours when Arizona is on MST and three hours when PST.
This returns time EST.
Well, Date.toString() will, if you're in EST at the moment. It's not part of the data stored within the Date - that's just an instant in time, with no idea what time zone or calendar system it might have started off in.
Your first two lines would be more simply written as:
Date currentDate = new Date();
You should use a DateFormat to convert the Date into a String. You can specify the time zone you want to use there. Do not start performing any arithmetic on the date yourself to add/remove offsets - that's a sign that you're heading in the wrong direction.

Converting milliseconds to Date object

I am having following code to convert milliseconds to Android Date object.
Date dateObj = new Date(milli);
But problem is that my milliseconds value is having GMT value added in it before i pass it to Date class, add when i print this date object i can see that date object is again adding GMT value in the milliseconds value and because of that my date is displayed as wrong.
So how can i generate Date object with out considering GMT value in it.
For example my milliseconds are 1385569800000 which is getting printed as below:
Wed, 27 Nov 2013 22:00:00 --> +5.30
But the current value of this time stamp without adding GMT is:
Wed, 27 Nov 2013 16:30:00
*UPDAE*
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
So basically i want to create Date object which has proper date time value in it with out adding extra GMT time added in it.
A Date is always in UTC. No need to change that.
When printing the date value, use SimpleDateFormat and call setTimeZone() on it before formatting the output string.
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
TimerTask is just a task and not its scheduling. Timer accepts a Date object for scheduling. The Date is in UTC there as well.
try my code if you a
long currentTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
currentTime -= offsetInMillis;
Date date = new Date(currentTime);
it is work for me
You can try with joda-time API.
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.
http://joda-time.sourceforge.net/key_instant.html
A Date object simply represents a moment in time. Imagine you're on the phone to someone on a different continent, and you say "3...2...1...NOW!". That "NOW" is the same moment for both of you, even though for one person it's 9am and for the other it's 4pm.
You're creating a Date representing the moment 1385569800000 milliseconds after the Java epoch (the beginning of 1970, GMT). That is your "NOW", and it's fixed and unchanging. What it looks like converted into text, however, depends on which timezone you want to display it for. Java defaults to using GMT, which would be right if you were in Britain during the winter, but for (I'm guessing) India you want it in a different time zone. Laalto's answer shows you how to do that.
here is the code,that worked like charm for me:
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}

Android Calendar has always the same date

i want an alarm to go off when user checks in a checkbox. Here is my code:
if (cb1.isChecked())
{
Calendar calA = Calendar.getInstance();
//calA.set(Calendar.YEAR, Calendar.YEAR);
//calA.set(Calendar.MONTH, Calendar.MONTH);
//calA.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH);
calA.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
calA.set(Calendar.MINUTE, Calendar.MINUTE);
calA.set(Calendar.SECOND, Calendar.SECOND);
calA.set(Calendar.MILLISECOND, Calendar.MILLISECOND);
alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), pendingIntentA);
Toast.makeText(main.this, "Set time: " + String.valueOf(calA.getTime()), Toast.LENGTH_SHORT).show();
}
Other codes are working fine, and if i set the hour and minute to specific ones, like
calA.set(Calendar.HOUR_OF_DAY, 15);
calA.set(Calendar.MINUTE, 24);
it's working, but with this code i always get this toast message:
Sat Mar 05 11:12:00 or Sat Mar 05 11:12:13
(neither the date nor the time is good)
What is wrong with my code?
Calendar.HOUR_OF_DAY is a constant, which just so happens to be an integer.
When you
calA.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
you are in fact setting the hour of day to whatever number happens to have been chosen for this constant. This value has no real meaning in relation to the hour of day, so it'll produce a garbage result.
When you get the calendar, it's by default set to the current time, so if that's what you're going for, simply don't set the time:
Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:
Calendar rightNow = Calendar.getInstance();
If you then want to set a time like "5 minutes into the future", do something like:
calA.add(Calendar.MINUTE, 5);
If you still get an incorrect time, verify that the system time is set correctly.
Source:
Calendar documentation
Calendar calA = Calendar.getInstance(); returns a calendar object whose locale is based on system settings and whose time fields have been initialized with the current date and time.
Sebastian P is right that Calendar.HOUR_OF_DAY is a constant, a key/index used for referencing the actual hour value.
See http://developer.android.com/reference/java/util/Calendar.html

Categories

Resources