Example usage for java.util Calendar setTimeInMillis

List of usage examples for java.util Calendar setTimeInMillis

Introduction

In this page you can find the example usage for java.util Calendar setTimeInMillis.

Prototype

public void setTimeInMillis(long millis) 

Source Link

Document

Sets this Calendar's current time from the given long value.

Usage

From source file:com.intuit.tank.util.TimeFormatUtil.java

public static String formatTime(int numSeconds) {
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    c.setTimeInMillis(0);
    c.add(Calendar.SECOND, numSeconds);
    return DF.format(c);
}

From source file:Main.java

public static String getDateString(long time) {
    Calendar date = Calendar.getInstance();
    time = time * 1000;/*  ww w  .j  av  a  2s .  com*/
    long targetTime = time - TimeZone.getDefault().getRawOffset();
    date.setTimeInMillis(targetTime);

    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = dateformat.format(date.getTime());
    return dateString;
}

From source file:com.kappaware.logtrawler.Utils.java

public static String printIsoDateTime(Long ts) {
    if (ts != null) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(ts);
        return DatatypeConverter.printDateTime(c);
    } else {/*from  www .j  a  v a  2s . co m*/
        return null;
    }
}

From source file:Main.java

/**
 * Calculates how many months are in given period. If one of the dates only partially covers the month, it still counts as full month.
 *
 * @param start Start of the period./*from w ww . j  av a  2s . co m*/
 * @param end   End of the period.
 * @return Number of days in given period. If {@code end < start}, returns -1.
 */
public static int getMonthCountInPeriod(long start, long end) {
    if (end < start)
        return -1;

    final Calendar cal = Calendar.getInstance();
    final int monthCountInYear = cal.getMaximum(Calendar.MONTH);

    cal.setTimeInMillis(start);
    final int startYear = cal.get(Calendar.YEAR);
    final int startMonth = cal.get(Calendar.MONTH) + 1;

    cal.setTimeInMillis(end);
    final int endYear = cal.get(Calendar.YEAR);
    final int endMonth = cal.get(Calendar.MONTH) + 1;

    int monthsCount;

    if (startYear != endYear) {
        monthsCount = monthCountInYear * Math.max(0, endYear - startYear - 1);
        monthsCount += monthCountInYear - startMonth + 1;
        monthsCount += endMonth;
    } else {
        monthsCount = endMonth - startMonth + 1;
    }

    return monthsCount;
}

From source file:Main.java

public static String getCurrentMonth() {
    Calendar calendar = Calendar.getInstance();
    long currentTime = calendar.getTimeInMillis();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
    calendar.setTimeInMillis(currentTime);
    return formatter.format(calendar.getTime());
}

From source file:Main.java

public static String convertToHumanReadableTime(Date givenDate, long currentTimeLong) {

    Calendar currentTime = Calendar.getInstance();
    currentTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    currentTime.setTimeInMillis(currentTimeLong);

    Calendar givenTime = Calendar.getInstance();
    givenTime.setTimeZone(TimeZone.getTimeZone("UTC"));
    givenTime.setTime(givenDate);//from  www. jav a2 s  .c  o m

    // Step 1: To see if time difference is less than 24 hours of not
    long timeDiff = currentTime.getTimeInMillis() - givenTime.getTimeInMillis();
    if (timeDiff <= 0) {
        return "Now";
    }

    String humanString = null;
    // Checking if timeDiff is less than 24 or not
    if ((timeDiff / TIME_DAY) >= 1) {
        // days
        int days = (int) (timeDiff / TIME_DAY);
        humanString = String.format(Locale.getDefault(), "%dd", days);
    } else {
        // checking if greater than hour
        if ((timeDiff / TIME_HOUR) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dh", (timeDiff / TIME_HOUR));
        } else if ((timeDiff / TIME_MINUTE) >= 1) {
            humanString = String.format(Locale.getDefault(), "%dm", (timeDiff / TIME_MINUTE));
        } else {
            humanString = String.format(Locale.getDefault(), "%ds", (timeDiff / TIME_MILLIS));
        }
    }

    return humanString;
}

From source file:Main.java

public static String findDateDifference(Long timestamp) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm a");
    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar1.setTimeInMillis(timestamp);
    calendar2.setTimeInMillis(System.currentTimeMillis());
    try {/*from   w  w  w  .j a v  a2 s  .  co  m*/

        //in milliseconds
        long diff = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        long diffYears = diffDays / 365;
        //            System.out.print(diffDays + " days, ");
        //            System.out.print(diffHours + " hours, ");
        //            System.out.print(diffMinutes + " minutes, ");
        //            System.out.print(diffSeconds + " seconds.");
        //            Log.e("difference is", diffDays + " days, " + diffHours + " hours, " + diffMinutes + " minutes, ");
        if (diffDays == 0) {
            if (diffHours > 0)
                return diffHours + "HOURS AGO";
            else if (diffMinutes > 0) {
                return diffMinutes + " MINS AGO";
                //return timeFormat.format(calendar1.getTime());
            } else
                return "Just Now";
        } else if (diffDays == 1) {
            return "Yesterday";
        } else if (diffDays <= 30) {
            return "" + diffDays + " DAYS AGO";
        } //else
          //return format.format(calendar1.getTime());

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.github.jgility.core.util.ReleasePlanningUtils.java

private static Calendar addDayOffset(final Calendar date, long offset) {
    Calendar newDate = new GregorianCalendar();
    newDate.setTimeInMillis(date.getTimeInMillis());
    newDate.add(Calendar.DAY_OF_MONTH, safeLongToInt(offset));
    return newDate;
}

From source file:com.microsoft.activitytracker.Classes.Utils.java

/**
 * pulls out the integer and converts it to a date. Then returns it in a
 * string format./*www.j  a v a 2  s . c o m*/
 * @param date the string that is returned from the odata call that contains the date
 *             looks something like "/Date(%i)/"
 * @return string with the date formatted like "MM/dd/yyyy"
 */
public static String getDatefromJsonDate(String date) {
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
    String[] split = date.split("[()]");

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(Long.parseLong(split[1]));

    return format.format(calendar.getTime());
}

From source file:ddf.metrics.reporting.internal.rrd4j.RrdDumper.java

private static String getCalendarTime(long timestamp) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp * 1000);

    String calTime = months[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + " "
            + calendar.get(Calendar.YEAR) + " ";

    calTime += addLeadingZero(calendar.get(Calendar.HOUR_OF_DAY)) + ":";
    calTime += addLeadingZero(calendar.get(Calendar.MINUTE)) + ":";
    calTime += addLeadingZero(calendar.get(Calendar.SECOND));

    return calTime;
}