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.augmentum.common.util.DateUtil.java

public static Date convertTimeZones(Calendar fromCal, String tzId) {
    Date date = null;/*from w w w .  j a v a2 s.  c om*/

    if ((fromCal != null) && StringUtils.isNotBlank(tzId)) {
        Calendar tzCal = new GregorianCalendar(TimeZone.getTimeZone(tzId));
        tzCal.setTimeInMillis(fromCal.getTimeInMillis());
        date = tzCal.getTime();
    }

    return date;
}

From source file:Main.java

public static int[] getTime(long time, String timeZone) {
    Calendar c = Calendar.getInstance();
    if (timeZone != null && timeZone.length() != 0) {
        TimeZone tz = TimeZone.getTimeZone(timeZone);
        c = Calendar.getInstance(tz);
    }/* w w w.  j a  v a 2s  .  c  o  m*/
    c.setTimeInMillis(time);
    int y = c.get(Calendar.YEAR);
    int m = c.get(Calendar.MONTH);
    int d = c.get(Calendar.DAY_OF_MONTH);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int second = c.get(Calendar.SECOND);
    return new int[] { y, m, d, hour, minute, second };
}

From source file:net.firejack.platform.core.utils.DateUtils.java

/**
 * Converts the date to the number of months since the epoch
 * @param date - date to be converted//from  w w  w . ja va  2 s.  com
 * @return number of months since the epoch
 */
public static int date2EpochMonths(Date date) {
    Calendar epoch = new GregorianCalendar();
    epoch.setTimeInMillis(0);

    Calendar dateCal = new GregorianCalendar();
    dateCal.setTime(date);

    int years = dateCal.get(Calendar.YEAR) - epoch.get(Calendar.YEAR);
    int months = dateCal.get(Calendar.MONTH);

    return years * 12 + months + 1; // months are zero-based, each year has 12 months always
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static String getFormatedDate(String rawDate, Boolean flag) {
    Calendar currenttime = Calendar.getInstance();
    Calendar commentTime = Calendar.getInstance();

    long nextDateInMillis = currenttime.getTimeInMillis();
    long commentTimeInMillis = Long.parseLong(rawDate) * 1000;
    commentTime.setTimeInMillis(commentTimeInMillis);

    long timeDifferenceMilliseconds = nextDateInMillis - commentTimeInMillis;
    long diffSeconds = timeDifferenceMilliseconds / 1000;
    long diffMinutes = timeDifferenceMilliseconds / (60 * 1000);
    long diffHours = timeDifferenceMilliseconds / (60 * 60 * 1000);
    long diffDays = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24);

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
    if (flag) {/*from   w w w.ja v a 2 s.  c o  m*/
        if (diffSeconds < 1) {
            return "less than a second";
        } else if (diffMinutes < 1) {
            return diffSeconds + "s";
        } else if (diffHours < 1) {
            return diffMinutes + "m";
        } else if (diffDays < 1) {
            return diffHours + "h";
        } else if (diffDays < 7) {
            return diffDays + "d";
        } else {
            return sdf.format(commentTime.getTime());
        }
    } else {
        if (diffSeconds < 1) {
            return "Posted Just Now";
        } else if (diffMinutes < 1) {
            if (diffSeconds == 1)
                return "Posted " + diffSeconds + " Second ago";
            else
                return "Posted " + diffSeconds + " Seconds ago";

        } else if (diffHours < 1) {
            if (diffMinutes == 1)
                return "Posted " + diffMinutes + " Month ago";
            else
                return "Posted " + diffMinutes + " Months ago";
        } else if (diffDays < 1) {
            if (diffHours == 1)
                return "Posted " + diffHours + " hour ago";
            else
                return "Posted " + diffHours + " hours ago";
        } else if (diffDays < 7) {
            if (diffDays == 1)
                return "Posted " + diffDays + " day ago";
            else
                return "Posted " + diffDays + " days ago";
        } else {

            return "Posted on " + sdf.format(commentTime.getTime());
        }
    }
}

From source file:DateUtils.java

/**
 * Get unix style date string.//  w  ww  .j a va  2  s  .  com
 */
public final static String getUnixDate(long millis) {
    if (millis < 0) {
        return "------------";
    }

    StringBuffer sb = new StringBuffer(16);
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(millis);

    // month
    sb.append(MONTHS[cal.get(Calendar.MONTH)]);
    sb.append(' ');

    // day
    int day = cal.get(Calendar.DATE);
    if (day < 10) {
        sb.append(' ');
    }
    sb.append(day);
    sb.append(' ');

    long sixMonth = 15811200000L; // 183L * 24L * 60L * 60L * 1000L;
    long nowTime = System.currentTimeMillis();
    if (Math.abs(nowTime - millis) > sixMonth) {

        // year
        int year = cal.get(Calendar.YEAR);
        sb.append(' ');
        sb.append(year);
    } else {

        // hour
        int hh = cal.get(Calendar.HOUR_OF_DAY);
        if (hh < 10) {
            sb.append('0');
        }
        sb.append(hh);
        sb.append(':');

        // minute
        int mm = cal.get(Calendar.MINUTE);
        if (mm < 10) {
            sb.append('0');
        }
        sb.append(mm);
    }
    return sb.toString();
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static String getFormatedDate(String rawDate, Boolean flag) {
    Calendar currenttime = Calendar.getInstance();
    Calendar commentTime = Calendar.getInstance();

    long nextDateInMillis = currenttime.getTimeInMillis();
    long commentTimeInMillis = Long.parseLong(rawDate) * 1000;
    commentTime.setTimeInMillis(commentTimeInMillis);

    long timeDifferenceMilliseconds = nextDateInMillis - commentTimeInMillis;
    long diffSeconds = timeDifferenceMilliseconds / 1000;
    long diffMinutes = timeDifferenceMilliseconds / (60 * 1000);
    long diffHours = timeDifferenceMilliseconds / (60 * 60 * 1000);
    long diffDays = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24);
    // long diffWeeks = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 *
    // 7);/*from www.  j  a v  a  2s.  c o m*/
    // long diffMonths = (long) (timeDifferenceMilliseconds / (60 * 60 *
    // 1000 * 24 * 30.41666666));
    // long diffYears = (long) (timeDifferenceMilliseconds / (60 * 60 * 1000
    // * 24 * 365));

    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd");
    if (flag) {
        if (diffSeconds < 1) {
            return "less than a second";
        } else if (diffMinutes < 1) {
            return diffSeconds + "s";
        } else if (diffHours < 1) {
            return diffMinutes + "m";
        } else if (diffDays < 1) {
            return diffHours + "h";
        } else if (diffDays < 7) {
            return diffDays + "d";
        } else {
            return sdf.format(commentTime.getTime());
        }
    } else {
        if (diffSeconds < 1) {
            return "Posted Just Now";
        } else if (diffMinutes < 1) {
            if (diffSeconds == 1)
                return diffSeconds + " second ago";
            else
                return diffSeconds + " seconds ago";

        } else if (diffHours < 1) {
            if (diffMinutes == 1)
                return diffMinutes + " minute ago";
            else
                return diffMinutes + " minutes ago";
        } else if (diffDays < 1) {
            if (diffHours == 1)
                return diffHours + " hour ago";
            else
                return diffHours + " hours ago";
        } else if (diffDays < 7) {
            if (diffDays == 1)
                return diffDays + " day ago";
            else
                return diffDays + " days ago";
        } else {

            return sdf.format(commentTime.getTime());
        }
    }
}

From source file:DateUtils.java

/**
 * Get ISO 8601 timestamp./* w w w . ja  va  2  s  .c o  m*/
 */
public final static String getISO8601Date(long millis) {
    StringBuffer sb = new StringBuffer(19);
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(millis);

    // year
    sb.append(cal.get(Calendar.YEAR));

    // month
    sb.append('-');
    int month = cal.get(Calendar.MONTH) + 1;
    if (month < 10) {
        sb.append('0');
    }
    sb.append(month);

    // date
    sb.append('-');
    int date = cal.get(Calendar.DATE);
    if (date < 10) {
        sb.append('0');
    }
    sb.append(date);

    // hour
    sb.append('T');
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if (hour < 10) {
        sb.append('0');
    }
    sb.append(hour);

    // minute
    sb.append(':');
    int min = cal.get(Calendar.MINUTE);
    if (min < 10) {
        sb.append('0');
    }
    sb.append(min);

    // second
    sb.append(':');
    int sec = cal.get(Calendar.SECOND);
    if (sec < 10) {
        sb.append('0');
    }
    sb.append(sec);

    return sb.toString();
}

From source file:DateUtils.java

/**
 * Get FTP date.//from w ww  . jav a  2 s . c  o  m
 */
public final static String getFtpDate(long millis) {
    StringBuffer sb = new StringBuffer(20);
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(millis);

    // year
    sb.append(cal.get(Calendar.YEAR));

    // month
    int month = cal.get(Calendar.MONTH) + 1;
    if (month < 10) {
        sb.append('0');
    }
    sb.append(month);

    // date
    int date = cal.get(Calendar.DATE);
    if (date < 10) {
        sb.append('0');
    }
    sb.append(date);

    // hour
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    if (hour < 10) {
        sb.append('0');
    }
    sb.append(hour);

    // minute
    int min = cal.get(Calendar.MINUTE);
    if (min < 10) {
        sb.append('0');
    }
    sb.append(min);

    // second
    int sec = cal.get(Calendar.SECOND);
    if (sec < 10) {
        sb.append('0');
    }
    sb.append(sec);

    // millisecond
    sb.append('.');
    int milli = cal.get(Calendar.MILLISECOND);
    if (milli < 100) {
        sb.append('0');
    }
    if (milli < 10) {
        sb.append('0');
    }
    sb.append(milli);
    return sb.toString();
}

From source file:DateHelper.java

/**
 * Calculating age from a current date//from  w w  w  .  ja  va2 s .c o  m
 * 
 * @param current
 * @param birthdate
 * @return Age from the current (arg) date
 */
public static float getAge(final Date current, final Date birthdate) {

    if (birthdate == null) {
        return 0;
    }
    if (current == null) {
        return getAge(birthdate);
    } else {
        final Calendar calend = new GregorianCalendar();
        calend.set(Calendar.HOUR_OF_DAY, 0);
        calend.set(Calendar.MINUTE, 0);
        calend.set(Calendar.SECOND, 0);
        calend.set(Calendar.MILLISECOND, 0);

        calend.setTimeInMillis(current.getTime() - birthdate.getTime());

        float result = 0;
        result = calend.get(Calendar.YEAR) - 1970;
        result += (float) calend.get(Calendar.MONTH) / (float) 12;
        return result;
    }

}

From source file:it.geosolutions.tools.io.file.FileRemover.java

public static List<File> collectOlder(final long time, final int daysAgo, final File root) {
    if (daysAgo < 0) {
        return null;
    }//from ww  w.j a  v a 2 s . co m
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);
    int days = cal.get(Calendar.DAY_OF_YEAR);
    if (days >= daysAgo)
        cal.set(Calendar.DAY_OF_YEAR, days - daysAgo);
    else {
        // TODO use getActualMaximum for days
        cal.set(Calendar.DAY_OF_YEAR, (354 + (days - daysAgo)));
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
    }
    // cal.getTime().toString()
    final Collector coll = new Collector(FileFilterUtils.andFileFilter(FileFilterUtils.directoryFileFilter(),
            FileFilterUtils.ageFileFilter(cal.getTime(), true)), 1);
    return coll.collect(root);
}