Example usage for java.util Calendar setTime

List of usage examples for java.util Calendar setTime

Introduction

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

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:Main.java

/***********************************************************************************************
 * ...//  ww  w. jav  a2 s .  c  o  m
 * ********************************************************************************************/

public static String dateToFormattedString(Date date) {

    String[] suffixes =
            //  0     1     2     3     4     5     6     7     8     9
            { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
                    //10   11    12    13    14    15    16    17    18    19
                    "th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
                    //20   21    22    23    24    25    26    27    28    29
                    "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
                    //30   31
                    "th", "st" };

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int day = calendar.get(Calendar.DAY_OF_MONTH);
    String dayStr = day + suffixes[day];

    SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("EEEE");
    String formattedDateDay = simpleDateFormatDay.format(date);

    SimpleDateFormat simpleDateFormatRest = new SimpleDateFormat("LLLL");// yyyy");
    String formattedDateRest = simpleDateFormatRest.format(date);

    return formattedDateDay + ", " + dayStr + " " + formattedDateRest;
}

From source file:Main.java

/**
 * Packs java time value into an MS-DOS time value.
 * @param time the time value/*w w w.  ja v a 2s . c  om*/
 * @return the MS-DOS packed time
 */
public static int packTime(long time) {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date(time));

    int seconds = c.get(Calendar.SECOND);
    int minutes = c.get(Calendar.MINUTE);
    int hours = c.get(Calendar.HOUR_OF_DAY);

    /*
     * Here is how MS-DOS packs a time value:
     * 0-4: seconds (divided by 2 because we only have 5 bits = 32 different numbers)
     * 5-10: minutes (6 bits = 64 possible values)
     * 11-15: hours (5 bits = 32 possible values)
     *
     * source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247(v=vs.85).aspx
     */
    return (hours << 11) | (minutes << 5) | (seconds / 2);
}

From source file:Main.java

public static Date getTimeCurrent() {
    Calendar cal = Calendar.getInstance();

    Date timeNow = new Date();

    cal.setTime(timeNow);

    // Um numero negativo vai decrementar a data
    cal.add(Calendar.MILLISECOND, TimeZone.getDefault().getOffset(timeNow.getTime()) * -1);

    return cal.getTime();
}

From source file:Main.java

public static boolean sameDate(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);//w  w w.j  a v  a  2  s.c om
    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

/**
 * Formats a date value in a string, assuming UTC timezone and Canada locale.
 * This method should be used only for occasional formatting.
 *
 * @param  date The date to format, or {@code null}.
 * @return The formatted date, or {@code null} if the given date was null.
 *
 * @see DatatypeConverter#printDateTime(Calendar)
 *
 * @since 3.06//from  w w w. j ava 2 s .  c om
 */
public static String printDateTime(final Date date) {
    if (date == null) {
        return null;
    }
    final Calendar calendar = CALENDAR.get();
    calendar.setTime(date);
    return DatatypeConverter.printDateTime(calendar);
}

From source file:Main.java

public static boolean datesOnSameDay(Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(date1);
    cal2.setTime(date2);/*from w w  w  .jav a  2  s  .  co  m*/
    return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
            && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}

From source file:Main.java

/**
 * Returns ordinal form of the day of month.
 * 1 = 1st//  w ww. j  a v  a  2 s .  com
 * 2 = 2nd
 *
 * @param date
 * @return ordinal day of month
 */
public static String getOrdinalDayOfMonth(Date date) {
    Calendar cal = Calendar.getInstance(Locale.US);
    cal.setTime(date);
    return getOrdinalDayOfMonth(cal);
}

From source file:Main.java

/**
 * Returns the month and release date of the movie
 *
 * @param releaseDate release date in yyyy-MM-dd format
 * @return month and year// w  w  w. java2 s .  c  o m
 */
@NonNull
public static String getDisplayReleaseDate(String releaseDate) {
    if (TextUtils.isEmpty(releaseDate)) {
        return "";
    }
    try {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DATE_FORMAT.parse(releaseDate));
        return calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + " "
                + calendar.get(Calendar.YEAR);
    } catch (ParseException e) {
        return "";
    }
}

From source file:Util.java

public static boolean isMidnight(long time) {
    Calendar calendar = Calendar.getInstance();

    calendar.setTime(new Date(time));
    return isMidnight(calendar);
}

From source file:Main.java

public static String getDateFromUTC(long timestamp) {
    if (timestamp == 0)
        timestamp = System.currentTimeMillis();
    Date date = new Date(timestamp);
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    cal.setTime(date);
    String val = (cal.get(Calendar.MONTH) + "/" + cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR) + ":"
            + cal.get(Calendar.MINUTE) + (cal.get(Calendar.AM_PM) == 0 ? "AM" : "PM"));

    //      final Date currentTime = new Date();
    //      final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy hh:mm:ss a z");
    //      sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    //      String ret = "UTC time: " + sdf.format(currentTime);
    //      System.out.println(ret);

    return val;
}