Example usage for android.text.format Time getJulianDay

List of usage examples for android.text.format Time getJulianDay

Introduction

In this page you can find the example usage for android.text.format Time getJulianDay.

Prototype

public static int getJulianDay(long millis, long gmtoff) 

Source Link

Document

Computes the Julian day number for a point in time in a particular timezone.

Usage

From source file:Main.java

public static int getJulianDay() {
    Time dayTime = new Time();
    dayTime.setToNow();//from   w  ww .  j  av  a2  s .c  o  m
    return Time.getJulianDay(System.currentTimeMillis(), dayTime.gmtoff);
}

From source file:Main.java

public static long getNumberOfDaysPassed(long date1, long date2) {
    Time sThenTime = new Time();
    sThenTime.set(date1);//from   ww  w.j a  v  a2 s  .c  om
    int day1 = Time.getJulianDay(date1, sThenTime.gmtoff);
    sThenTime.set(date2);
    int day2 = Time.getJulianDay(date2, sThenTime.gmtoff);
    return Math.abs(day2 - day1);
}

From source file:Main.java

private static int getIntervalInDays(long startMillis, long endMillis, Time timeObj) {
    timeObj.set(startMillis);/*from  w ww . j  a  v a  2  s  .com*/
    int startDay = Time.getJulianDay(startMillis, timeObj.gmtoff);
    timeObj.set(endMillis);
    return Time.getJulianDay(endMillis, timeObj.gmtoff) - startDay;
}

From source file:Main.java

/**
 * Get formatted time.// ww w .  j a v  a 2s .c om
 *
 * @param publishedTime The published time in millis.
 *
 * @return The formatted time.
 */
static public String getFormattedTime(long publishedTime) {
    // This is copied from RecentCallsListActivity.java

    long now = System.currentTimeMillis();

    // Set the date/time field by mixing relative and absolute times.
    int flags = DateUtils.FORMAT_ABBREV_ALL;

    if (!DateUtils.isToday(publishedTime)) {
        // DateUtils.getRelativeTimeSpanString doesn't consider the nature
        // days comparing with DateUtils.getRelativeDayString. Override the
        // real date to implement the requirement.

        Time time = new Time();
        time.set(now);
        long gmtOff = time.gmtoff;
        int days = Time.getJulianDay(publishedTime, gmtOff) - Time.getJulianDay(now, gmtOff);

        // Set the delta from now to get the correct display
        publishedTime = now + days * DateUtils.DAY_IN_MILLIS;
    } else if (publishedTime > now && (publishedTime - now) < DateUtils.HOUR_IN_MILLIS) {
        // Avoid e.g. "1 minute left" when publish time is "07:00" and
        // current time is "06:58"
        publishedTime += DateUtils.MINUTE_IN_MILLIS;
    }

    return (DateUtils.getRelativeTimeSpanString(publishedTime, now, DateUtils.MINUTE_IN_MILLIS, flags))
            .toString();
}

From source file:Main.java

/**
 * Determine the difference, in days between two dates.  Uses similar logic as the
 * {@link android.text.format.DateUtils.getRelativeTimeSpanString} method.
 *
 * @param time Instance of time object to use for calculations.
 * @param date1 First date to check./*from   ww  w .  jav a2 s.c  om*/
 * @param date2 Second date to check.
 * @return The absolute difference in days between the two dates.
 */
public static int getDayDifference(Time time, long date1, long date2) {
    time.set(date1);
    int startDay = Time.getJulianDay(date1, time.gmtoff);

    time.set(date2);
    int currentDay = Time.getJulianDay(date2, time.gmtoff);

    return Math.abs(currentDay - startDay);
}

From source file:com.example.cal.mysunshine.Utility.java

/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 *
 * @param context Context to use for resource localization
 * @param dateInMillis The date in milliseconds
 * @return a user-friendly representation of the date.
 */// w  w  w  .j  a  va2  s. co  m
public static String getFriendlyDayString(Context context, long dateInMillis) {
    // The day string for forecast uses the following logic:
    // For today: "Today, June 8"
    // For tomorrow:  "Tomorrow"
    // For the next 5 days: "Wednesday" (just the day name)
    // For all days after that: "Mon Jun 8"

    Time time = new Time();
    time.setToNow();
    long currentTime = System.currentTimeMillis();
    int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff);
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff);

    // If the date we're building the String for is today's date, the format
    // is "Today, June 24"
    if (julianDay == currentJulianDay) {
        String today = context.getString(R.string.today);
        int formatId = R.string.format_full_friendly_date;
        return String.format(context.getString(formatId), today, getFormattedMonthDay(context, dateInMillis));
    } else if (julianDay < currentJulianDay + 7) {
        // If the input date is less than a week in the future, just return the day name.
        return getDayName(context, dateInMillis);
    } else {
        // Otherwise, use the form "Mon Jun 3"
        SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd");
        return shortenedDateFormat.format(dateInMillis);
    }
}

From source file:com.joeyturczak.jtscanner.utils.Utility.java

public static long normalizeDate(long startDate) {
    // normalize the start date to the beginning of the (UTC) day
    Time time = new Time();
    time.set(startDate);/*from   w ww .j ava2  s .  co m*/
    int julianDay = Time.getJulianDay(startDate, time.gmtoff);
    return time.setJulianDay(julianDay);
}

From source file:com.ewintory.footballscores.ui.adapter.PagerAdapter.java

public String getDayName(Context context, long dateInMillis) {
    // If the date is today, return the localized version of "Today" instead of the actual
    // day name./*  ww w  .ja v a  2 s.  c  o m*/

    Time t = new Time();
    t.setToNow();
    int julianDay = Time.getJulianDay(dateInMillis, t.gmtoff);
    int currentJulianDay = Time.getJulianDay(System.currentTimeMillis(), t.gmtoff);
    if (julianDay == currentJulianDay) {
        return context.getString(R.string.today);
    } else if (julianDay == currentJulianDay + 1) {
        return context.getString(R.string.tomorrow);
    } else if (julianDay == currentJulianDay - 1) {
        return context.getString(R.string.yesterday);
    } else {
        Time time = new Time();
        time.setToNow();
        // Otherwise, the format is just the day of the week (e.g "Wednesday".
        return DAY_FORMAT.format(dateInMillis);
    }
}

From source file:com.example.android.sunshine.app.Utility.java

/**
 * Helper method to convert the database representation of the date into something to display
 * to users.  As classy and polished a user experience as "20140102" is, we can do better.
 *
 * @param context Context to use for resource localization
 * @param dateInMillis The date in milliseconds
 * @return a user-friendly representation of the date.
 *///from ww w . j av  a  2 s.c o m
public static String getFriendlyDayString(Context context, long dateInMillis, boolean displayLongToday) {
    // The day string for forecast uses the following logic:
    // For today: "Today, June 8"
    // For tomorrow:  "Tomorrow"
    // For the next 5 days: "Wednesday" (just the day name)
    // For all days after that: "Mon Jun 8"

    Time time = new Time();
    time.setToNow();
    long currentTime = System.currentTimeMillis();
    int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff);
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff);

    // If the date we're building the String for is today's date, the format
    // is "Today, June 24"
    if (displayLongToday && julianDay == currentJulianDay) {
        String today = context.getString(R.string.today);
        int formatId = R.string.format_full_friendly_date;
        return String.format(context.getString(formatId, today, getFormattedMonthDay(context, dateInMillis)));
    } else if (julianDay < currentJulianDay + 7) {
        // If the input date is less than a week in the future, just return the day name.
        return getDayName(context, dateInMillis);
    } else {
        // Otherwise, use the form "Mon Jun 3"
        SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd");
        return shortenedDateFormat.format(dateInMillis);
    }
}

From source file:com.appsimobile.appsii.module.appsiagenda.MonthAdapter2.java

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    MonthView v = (MonthView) holder.itemView;
    SimpleArrayMap<String, Integer> drawingParams;
    // We store the drawing parameters in the view so it can be recycled
    drawingParams = (SimpleArrayMap<String, Integer>) v.getTag();

    if (drawingParams == null) {
        drawingParams = new SimpleArrayMap<>();
    }// www .ja v  a2s  .c  om
    drawingParams.clear();

    final int month = position % MONTHS_IN_YEAR;
    final int year = position / MONTHS_IN_YEAR + mController.getMinYear();

    int selectedDay = -1;
    if (isSelectedDayInMonth(year, month)) {
        selectedDay = mSelectedDay.day;
    }

    // Invokes requestLayout() to ensure that the recycled view is set with the appropriate
    // height/number of weeks before being displayed.
    v.reuse();
    mTime.set(1, month, year);
    long millis = mTime.normalize(true);
    int startJulianDay = Time.getJulianDay(millis, 0);

    drawingParams.put(MonthView.VIEW_PARAMS_SELECTED_DAY, selectedDay);
    drawingParams.put(MonthView.VIEW_PARAMS_YEAR, year);
    drawingParams.put(MonthView.VIEW_PARAMS_MONTH, month);
    drawingParams.put(MonthView.VIEW_PARAMS_START_JULIAN_DAY, startJulianDay);
    drawingParams.put(MonthView.VIEW_PARAMS_WEEK_START, mController.getFirstDayOfWeek());
    v.setMonthParams(drawingParams);
    v.invalidate();
}