0

hey guys im using android time to get my data of shealth I m getting first date of week and month's correctly but im not getting time of start of day im getting output like this

start: Thu, 1 Feb 2019 18:30:00 UTC 6:30 pm  EndSun, 31 Mar 2019 12:47:15 UTC 12:47 pm

I want to get that time in

enter code here
start: Thu, 1 Feb 2019 00:00:00 UTC 00:00 pm  End Sun, 31 Mar 2019 23:59:59 UTC 11:59 pm

im using that function to get time of week and month

public static long getStartTimeOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.DAY_OF_WEEK, 1);

        return  cal.getTimeInMillis()+ ONE_DAY;
    }

for month :

public static long getMonthDateFirstdate(){
        Calendar cal = Calendar.getInstance();
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);
        cal.set(Calendar.DATE,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTimeInMillis();
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Tanveerbyn
  • 764
  • 1
  • 10
  • 25
  • 1
    Consider not using `Calendar`. That class is poorly designed and long outdated and therefore a poor tool for not quite trivial date and time operations like yours. Instead [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) has much better support for what you are doing and is also generally so much nicer to work with. – Ole V.V. Mar 20 '19 at 00:23
  • While your code isn’t quite correct, I cannot reproduce the output you report. I get `Mon Mar 25 00:00:00 UTC 2019` and `Fri Mar 01 00:00:00 UTC 2019` (assuming `private static final long ONE_DAY = TimeUnit.DAYS.toMillis(1);`). And if it’s best to understand *data of shealth*, you may want to explain? – Ole V.V. Mar 20 '19 at 00:32
  • Yaa i know that's weird tht time is showing 6:30 pm n all... – Tanveerbyn Mar 28 '19 at 21:10
  • I think that problem is happening because of gmt/utc to ist time conversion is it? – Tanveerbyn Mar 28 '19 at 21:15

3 Answers3

0

Try this:

Date date = new Date();
Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.DAY_OF_WEEK, 1);

    return  cal.getTimeInMillis()+ ONE_DAY;
0

Try SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss")

  • While I disagree about using the notoriously troublesome `SimpleDateFormat` class I also fail to see how it answers the question asked? – Ole V.V. Mar 20 '19 at 01:28
0

java.time

I suggest the following implementations of your two methods.

public static long getStartTimeOfWeek() {
    WeekFields wf = WeekFields.of(Locale.getDefault());
    return LocalDate.now(ZoneOffset.UTC)
            .with(wf.dayOfWeek(), 1)
            .atStartOfDay(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();
}

public static long getMonthDateFirstdate(){
    return YearMonth.now(ZoneOffset.UTC)
            .atDay(1)
            .atStartOfDay(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();
}

To try them out:

    System.out.println(Instant.ofEpochMilli(getStartTimeOfWeek()));
    System.out.println(Instant.ofEpochMilli(getMonthDateFirstdate()));

When I ran these two lines just now on my computer I got:

2019-03-18T00:00:00Z
2019-03-01T00:00:00Z

Since both your expected and your observed output mentions UTC I assumed that you wanted the operations in UTC. And since Monday is the first day of week in my locale, I got Monday March 18 in the first line.

I suggest you avoid the Calendar class. That class is poorly designed and long outdated and therefore a poor tool for not quite trivial date and time operations like yours. Instead I am using java.time, the modern Java date and time API. It has much better support for what you are doing and is also generally so much nicer to work with.

What went wrong in your code?

It’s not completely clear to us why you got the results you got since you haven’t provided a complete example and since the behaviour of your code depends on time zone and locale. Potential issues include:

  • A likely explanation why you got 18:30:00 UTC 6:30 pm is that you ran your code in a time zone at UTC offset +05:30, for example Asia/Kolkata or Asia/Colombo. You’ve got the start of day in this time zone. It’s the same point in time as 18:30 on the previous day in UTC.
  • 12:47:15 UTC 12:47 pm looks weird. It may come from your constant ONE_DAY having an incorrect value.
  • cal.set(Calendar.DAY_OF_WEEK, 1); sets the day of week to 1 = Sunday (if I remember those constants correctly, I’m never sure), but in some locales the previous Sunday will be chosen, in other locales the next Sunday, and in yet other locales it will vary by current day of week.
  • It seems that in getMonthDateFirstdate you forgot to set hour of day. Easy to forget with the Calendar class, where you have to set each field individually (nice that you remembered the milliseconds, many forget those).
  • Use a half-open interval. Instead of wanting the end to be Sun, 31 Mar 2019 23:59:59 UTC 11:59 pm it’s recommended to set it to Mon, 01 Apr 2019 00:00:00 UTC 12:00 am and use this as an open end-point, that is, your interval is from the start time inclusive to your end time exclusive. So this means that a point in time is within the interval only if it strictly before the end time.

Question: Can I use java.time on Android API level 20?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • V. V THAnks for reply.. Your suggestion is good and accurate also but im using minimum api level 20 and it requires 26 which is oreo so my app is not compatible with most of devices that's why Im not using this.. If u have any other solution which is compatible for all devices... Tell me – Tanveerbyn Mar 28 '19 at 21:06
  • And one_day is 24*60*60*1000 – Tanveerbyn Mar 28 '19 at 21:07
  • Thanks for reporting back. You may want to read the subsection *Question: Can I use java.time on Android API level 20?* of the answer more closely (I just edited that header). 24*60*60*1000 looks right. It should probably have type `long`. – Ole V.V. Mar 29 '19 at 03:05