0

Since Time class is deprecated, I can no longer use the getJulianDay() in the following function. I want to use Calender or GregorianCalender class to achieve the same. I partly got my answer from [question]:Time getJulianDay() deprecated which alternatives to convert date to julian and vice versa? but I'm new to such classes so I don't know how to transform the GregorianCalender to long so that I can return it in the function. How can I get the Julian Date? Thanks!

public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    Time time = new Time();
    time.set(startDate);
    int julianDay = Time.getJulianDay(startDate, time.gmtoff);
    return time.setJulianDay(julianDay);
}
Community
  • 1
  • 1
Pulak
  • 768
  • 7
  • 16
  • There are [lots of questions and answers on SO](/search?q=%5Bjava%5D+julian+date) about Julian dates. What about them is unhelpful? For instance, [this one](http://stackoverflow.com/questions/20035403/julian-date-to-regular-date-conversion)? – T.J. Crowder Sep 30 '16 at 09:30

1 Answers1

0

Does this suits what you need?

public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    GregorianCalendar date = (GregorianCalendar)     GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
    date.setTime(new Date(startDate));
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);

    //transform your calendar to a long in the way you prefer
    return date.getTimeInMillis();
}
pleft
  • 7,567
  • 2
  • 21
  • 45