0

I read a lot of answers to similar questions, but I couldn't find what I intend to do.

I'm developing an android application that I need to check if the current device DateTime (variable TimeZone) are between two DateTimes (specified in UTC+0).

Here a pseudo example:

if( (UTC+0 DateTime of actual week friday at 23:00:00) 
    < 
    (device DateTime transformed to UTC+0)
    <
    (UTC+0 DateTime of actual week sunday at 23:00:00) )
{
    #some code here
}

I don't know how to obtain the first and third DateTimes, I can get the day with:

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
if(Calendar.FRIDAY < c.get(Calendar.DAY_OF_WEEK) < Calendar.SUNDAY)

but I still need the DateTime of the actual week friday and sunday at 23:00:00 specified in UTC+0.

I want to transform the actual device DateTime to UTC+0 before the comparision, I tried with Calendar and Joda Time utils but I couldn't find a simple way to obtain the current UTC+0 DateTime and also transform a non UTC+0 DateTime to UTC+0.

I'm sure it must be very simple but can't find the way.

Gerard Reches
  • 3,048
  • 3
  • 28
  • 39
  • Date objects don't care about timezone. – njzk2 Aug 01 '15 at 22:19
  • @njzk2 calendar.getTime() returns my device time, I'm in a UTC+1 TimeZone with DST. Gives me at this moment "sunday 00:21:00" and I want to get UTC+0 time, so I want "saturday 22:21:00". – Gerard Reches Aug 01 '15 at 22:25
  • 1
    that's the same date. it is just a matter of how you format it. – njzk2 Aug 01 '15 at 22:26
  • @njzk2 can you post an example please? – Gerard Reches Aug 01 '15 at 22:27
  • @njzk2 Do you mean that java understands that '2015-08-02 T00:50:00 GMT+0200' equals to '2015-08-01 T22:50:00 GMT+0000' ? – Gerard Reches Aug 01 '15 at 22:52
  • 1
    they are represented by exactly the same `Date` object, which is only a timestamp. The timezone is used only for the `Calendar` object, and when parsing and formatting dates – njzk2 Aug 02 '15 at 03:14

2 Answers2

2
DateTime date = DateTime.now();            
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (z)");

Output : Sun Jul 26 2015 04:33:19 GMT+0530 (IST)

this will give you some idea

EDITED

use this

DateTime currentTime = DateTime.now( DateTimeZone.UTC ); 

it will work you can initialize it with any timezone as

DateTime.now(DateTimeZone zone) 

try this also

DateTime currentTime = DateTime.now( DateTimeZone.getDefault() );
Aditya Chauhan
  • 271
  • 3
  • 13
  • This gives a non UTC/GMT+0 DateTime. How to transform to GMT+0 without knowing that your GMT is +0530? Or how to do that DateTime.now returns a GMT+0 DateTime? – Gerard Reches Aug 01 '15 at 22:39
  • looking into it..will get back to you soon – Aditya Chauhan Aug 01 '15 at 22:57
  • 1
    use this DateTime currentTime = DateTime.now( DateTimeZone.UTC ); it will work you can initialize it with any timezone as DateTime.now(DateTimeZone zone) try this also DateTime currentTime = DateTime.now( DateTimeZone.getDefault() ); – Aditya Chauhan Aug 01 '15 at 23:02
  • Works with this! DateTime currentTime= DateTime.now(DateTimeZone.forID("UTC")); – Gerard Reches Aug 01 '15 at 23:32
  • What "DateTime" class is this? What package? If this is Joda-Time, then this code is incorrect. You do not use SimpleDateFormat class with Joda-Time. Joda-Time has its own formatters. – Basil Bourque Aug 02 '15 at 04:56
  • DateTime is from jodatime and both belong to JodaTime these are the package:org.joda.time.format.DateTimeFormat;,org.joda.time.format.DateTimeFormatter; it works properly i have used it and it works great..you can use compile 'net.danlew:android.joda:2.8.1' as a better and more efficient version of JodaTime – Aditya Chauhan Aug 02 '15 at 05:07
  • @AdityaChauhan No, the [SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) shown in your Answer does *not* come from those packages you mentioned, is *not* a part of Joda-Time, and should *not* be used with [`DateTime`](http://www.joda.org/joda-time/apidocs/org/joda/time/field/DelegatedDurationField.html) objects. – Basil Bourque Aug 02 '15 at 05:53
1

Comparison Methods: isBefore, isAfter, isEqual

You are working too hard.

You need not make time zone adjustments when comparing DateTime objects. Joda-Time handles that for you automatically. Internally Joda-Time tracks a DateTime as a number of milliseconds fro the epoch in UTC. So comparing to see which comes before or later is easy.

Just call the comparison methods on DateTime, isBefore, isAfter, and isEqual.

Boolean inRange = ( x.isAfter( start ) || x.isEqual( start ) ) && x.isBefore ( stop ) ;

If the DateTime objects happen to all have the same time zone (DateTimeZone), you make use of the handy Interval class.

Interval interval = new Interval( start , stop ) ;
Boolean inRange = interval.contains( x ) ;

As for finding the next/last Monday, Sunday, whatever, search StackOverflow as that has been addressed before many times (like this), as have all the topics in your Question actually. Such as this and this and this.

Both my code above and the Interval class use the "Half-Open" approach in their comparison logic. The beginning is inclusive while the ending is exclusive. Search StackOverflow.com to learn more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks for explain it, now I understand it better :) – Gerard Reches Aug 02 '15 at 09:03
  • @GerardReches Good. In the future, please search StackOverflow.com before posting. In particular now, you should search for "half-open" to learn about the logic of the in-range testing in my code above and also used by that `Interval` class. – Basil Bourque Aug 02 '15 at 17:37