Android Long to Date Convert getTimeDelta(long delta, String dateFormat)

Here you can find the source of getTimeDelta(long delta, String dateFormat)

Description

Returns time delta from current time with `dateFormat`

License

Open Source License

Parameter

Parameter Description
delta millis of time delta
dateFormat Date format

Return

Formatted time delta string

Declaration

public static String getTimeDelta(long delta, String dateFormat) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static final String DATE_FORMAT = "yyyy-MM-dd G HH:mm:ss z";

    /**/*  w w w  .  j a v  a2s  .c  om*/
     * Returns time delta from current time with `dateFormat`
     * @param delta
     *       millis of time delta
     * @param dateFormat
     *       Date format
     * @return
     *      Formatted time delta string 
     */
    public static String getTimeDelta(long delta, String dateFormat) {
        Calendar current = Calendar.getInstance();
        current.setTimeInMillis(current.getTimeInMillis() + delta);
        return formatDate(current.getTime(), dateFormat);
    }

    /**
     * Format date with default `DATE_FORMAT` and default locale `Locale.KOREA`
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, DATE_FORMAT);
    }

    /**
     * Format date with `dateFormat` given and default locale `Locale.KOREA`
     * @param date
     * @param dateFormat
     * @return
     */
    public static String formatDate(Date date, String dateFormat) {
        return formatDate(date, dateFormat, Locale.ENGLISH);
    }

    /**
     * Format date
     * @param date
     *       `Date` formatted to
     * @param dateFormat
     *       Date format like 'yyyy-MM-dd'
     * @param locale
     *       Locale for date format.
     * @return
     *      Formatted date string 
     */
    public static String formatDate(Date date, String dateFormat,
            Locale locale) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat, locale);
        return format.format(date);
    }
}

Related

  1. getTimeString(long millis)
  2. parseTime(long time, String pattern)
  3. getDateByInt(long va)
  4. getDateTimeString(long dateTime)
  5. getLabelForEventInTime(long inTime)
  6. timeAgo(long aTime)
  7. isTomorrow(long lTime)
  8. getActivityTime(long startTime, long endTime)
  9. getDate(long timeStamp)