Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

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

Prototype

public GregorianCalendar() 

Source Link

Document

Constructs a default GregorianCalendar using the current time in the default time zone with the default Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

public static String getCurrMonthAfter(int monthDiff, String format) {
    SimpleDateFormat ft = new SimpleDateFormat(format);
    Calendar c = new GregorianCalendar();
    c.add(Calendar.MONTH, monthDiff);
    return ft.format(c.getTime());
}

From source file:Main.java

private static String getDayOfWeek(String format, int calendarField) {
    String strDate = null;/*from w w w  . ja v a2  s.  c  o  m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == calendarField) {
            strDate = mSimpleDateFormat.format(c.getTime());
        } else {
            int offectDay = calendarField - week;
            if (calendarField == Calendar.SUNDAY) {
                offectDay = 7 - Math.abs(offectDay);
            }
            c.add(Calendar.DATE, offectDay);
            strDate = mSimpleDateFormat.format(c.getTime());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Main.java

public static String getStringByDateFormat(String strDate, String format) {
    String mDateTime = null;//  w ww  . j a  va 2  s .  c  o m
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("MMM dd,yyyy kk:mm:ss aa", Locale.ENGLISH);
        c.setTime(mSimpleDateFormat.parse(strDate));
        SimpleDateFormat mSimpleDateFormat2 = new SimpleDateFormat(format);
        mDateTime = mSimpleDateFormat2.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

/**
 * Reset the time of a date/*from   ww w.  j a va2  s.  com*/
 * @param date the date with time to reset
 * @return the 0 time date.
 */
public static Date zeroTimeDate(Date date) {
    final GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTime(date);
    gregorianCalendar.set(Calendar.HOUR_OF_DAY, 0);
    gregorianCalendar.set(Calendar.MINUTE, 0);
    gregorianCalendar.set(Calendar.SECOND, 0);
    gregorianCalendar.set(Calendar.MILLISECOND, 0);
    return gregorianCalendar.getTime();
}

From source file:Main.java

public static String getCurrentDate(String format) {
    String curDateTime = null;//from  ww  w  .j  a  v a  2  s. c om
    try {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        Calendar c = new GregorianCalendar();
        curDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return curDateTime;

}

From source file:Main.java

public static String getCurrentDateByOffset(String format, int calendarField, int offset) {
    String mDateTime = null;//from  ww w  .  j a  v a  2  s .  co m
    try {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        Calendar c = new GregorianCalendar();
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;

}

From source file:Main.java

public static String getStringByOffset(String strDate, String format, int calendarField, int offset) {
    String mDateTime = null;//from  w  ww .  j  ava  2s  .  co  m
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        c.setTime(mSimpleDateFormat.parse(strDate));
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return mDateTime;
}

From source file:Main.java

static Calendar toNextWholeMinute(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());/*  w w  w  . j  av a2  s  .c  o m*/
    c.add(Calendar.MINUTE, 1);
    c.set(Calendar.SECOND, 0);
    return c;
}

From source file:Main.java

public static String getStringByOffset(Date date, String format, int calendarField, int offset) {
    String strDate = null;/*  ww  w  .j  av  a2s  . co m*/
    try {
        Calendar c = new GregorianCalendar();
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        c.setTime(date);
        c.add(calendarField, offset);
        strDate = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strDate;
}

From source file:Main.java

public static Date getDateDayBefore(Date originalDate, int days) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(originalDate);//  www.j  a  v a 2  s  .  co m
    cal.add(Calendar.DAY_OF_MONTH, days * -1);
    return cal.getTime();
}