Example usage for java.util Calendar DATE

List of usage examples for java.util Calendar DATE

Introduction

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

Prototype

int DATE

To view the source code for java.util Calendar DATE.

Click Source Link

Document

Field number for get and set indicating the day of the month.

Usage

From source file:Main.java

public static String getCurrentDateString() {
    Calendar calendar = Calendar.getInstance();
    return calendar.get(Calendar.YEAR) + "-" + String.format("%02d", (calendar.get(Calendar.MONTH) + 1)) + "-"
            + String.format("%02d", calendar.get(Calendar.DATE));
}

From source file:Main.java

public static String getBeforeTodayWithFormat(int gap, String dateFormat) {
    Calendar currentCalendar = Calendar.getInstance();
    currentCalendar.add(Calendar.DATE, gap);

    Date date = currentCalendar.getTime();
    SimpleDateFormat format = new SimpleDateFormat(dateFormat);
    return format.format(date);
}

From source file:Main.java

public static String getPreviousDate(int days) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -days);
    return sdf.format(cal.getTime());
}

From source file:Main.java

public static String getFirstDayOfMonth(int year, int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
    return new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
}

From source file:Main.java

public static String getDateString(long millitm) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(millitm);//from   w ww  .j a  va  2  s .c om
    return (setZeroPad(cal.get(Calendar.YEAR)) + "/" + setZeroPad(cal.get(Calendar.MONTH)) + "/"
            + setZeroPad(cal.get(Calendar.DATE)) + " " + setZeroPad(cal.get(Calendar.HOUR_OF_DAY)) + ":"
            + setZeroPad(cal.get(Calendar.MINUTE)) + ":" + setZeroPad(cal.get(Calendar.SECOND)));
}

From source file:DateUtils.java

public static Date getLastWeek() {
    GregorianCalendar dayBeforeThisWeek = new GregorianCalendar();
    int dayFromMonday = (dayBeforeThisWeek.get(Calendar.DAY_OF_WEEK) + 7 - Calendar.MONDAY) % 7;
    dayBeforeThisWeek.add(Calendar.DATE, -dayFromMonday - 1);
    return dayBeforeThisWeek.getTime();
}

From source file:Util.java

public static String formatAsMySQLDatetime(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//from w w w.  j av a  2  s.c  o m

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    int day = calendar.get(Calendar.DATE);

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

    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}

From source file:Main.java

public static String addDay(int day) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, day);
    return dateFormat.format(cal.getTime());

}

From source file:Main.java

public static String getDateFormattedRecent(Date date, int daysAgo) {

    //display day of the week for activities occurred in the last daysAgo
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 0 - daysAgo);

    Date weekAgo = calendar.getTime();
    if (date.after(weekAgo)) {
        return new SimpleDateFormat("EEE").format(date);//EEE, short version of day of the week
    }// www  .  ja  va  2s  . c o m
    //otherwise, display the date of the activity
    else {
        return new SimpleDateFormat("MM.dd.yy").format(date);
    }
}

From source file:Main.java

/**
 * This method adds days to a date//w w w.ja  v a2  s . c  o  m
 *
 * @param date the date.
 * @param days the number of days to add.
 */
public static Date getDateAfterAddition(Date date, int days) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);
    cal.add(Calendar.DATE, days);

    return cal.getTime();
}