Example usage for java.util GregorianCalendar set

List of usage examples for java.util GregorianCalendar set

Introduction

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

Prototype

public final void set(int year, int month, int date) 

Source Link

Document

Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

Usage

From source file:Main.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(1995, 10, 29); // Date set to 29th November 1999
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static String changeDateAsMonth(String date, int imonth) {
    GregorianCalendar calendar = new GregorianCalendar();
    if (date.length() == 8) {
        calendar.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(4, 6)) - 1,
                Integer.parseInt(date.substring(6, 8)));
    } else if (date.length() == 10) {
        calendar.set(Integer.parseInt(date.substring(0, 4)), Integer.parseInt(date.substring(5, 7)) - 1,
                Integer.parseInt(date.substring(8, 10)));
    }//w w  w .java  2  s.c o m
    calendar.add(GregorianCalendar.MONTH, imonth);
    String sDate[] = calendar.getTime().toLocaleString().split(" ");
    return sDate[0];
}

From source file:Main.java

public static String weekDay(String year, String month, String day) {
    String strWeekday = "";
    try {/*ww  w .  j  a v  a2  s. c om*/
        GregorianCalendar cal = new GregorianCalendar();
        cal.setLenient(false);
        cal.clear();
        cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
        strWeekday = String.valueOf(cal.get(Calendar.DAY_OF_WEEK) - 1);
    } catch (IllegalArgumentException e) {
        strWeekday = "";
    }
    return strWeekday;
}

From source file:org.toobsframework.biz.validation.CustomValidationUtils.java

/**
 * Given three integer inputs representing a date, checks to 
 * see if the specified date is after today's date.
 * @param day - int 1-31/*w w w . j  a v  a 2  s .com*/
 * @param month - int 1-12
 * @param year
 * @return
 */
public static boolean rejectIfDayMonthYearBeforeToday(Integer day, Integer month, Integer year) {
    if (day != null && month != null && year != null) {
        //construct Calendar object
        //set time fields with inputted parameters
        GregorianCalendar cal = new GregorianCalendar();
        // note month conversion: Calendar expects 0 based month param
        cal.set(year, month - 1, day);
        //get a date object out of the 
        Date closingDate = cal.getTime();

        //if the closing date is before right now,
        //then throw a validation error
        if (closingDate.before(new Date(System.currentTimeMillis()))) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

public static int compareDate(String aDate, String bDate) {

    String[] strDate;/* w  ww.  j  a v a  2 s . com*/
    GregorianCalendar aCal = new GregorianCalendar();
    GregorianCalendar bCal = new GregorianCalendar();
    if (aDate.indexOf("/") != -1) {
        strDate = aDate.split("/");
        aCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                Integer.parseInt(strDate[2].trim()));
    } else if (aDate.indexOf("-") != -1) {
        strDate = aDate.split("-");
        aCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                Integer.parseInt(strDate[2].trim()));
    } else if (aDate.length() == 8) {
        aCal.set(Integer.parseInt(aDate.substring(0, 4)), Integer.parseInt(aDate.substring(4, 6)) - 1,
                Integer.parseInt(aDate.substring(6, 8)));
    } else if (aDate.length() == 10) {
        aCal.set(Integer.parseInt(aDate.substring(0, 4)), Integer.parseInt(aDate.substring(5, 7)) - 1,
                Integer.parseInt(aDate.substring(8, 10)));
    }

    if (bDate.indexOf("/") != -1) {
        strDate = bDate.split("/");
        bCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                Integer.parseInt(strDate[2].trim()));
    } else if (bDate.indexOf("-") != -1) {
        strDate = bDate.split("-");
        bCal.set(Integer.parseInt(strDate[0].trim()), Integer.parseInt(strDate[1].trim()) - 1,
                Integer.parseInt(strDate[2].trim()));
    } else if (bDate.length() == 8) {
        bCal.set(Integer.parseInt(bDate.substring(0, 4)), Integer.parseInt(bDate.substring(4, 6)) - 1,
                Integer.parseInt(bDate.substring(6, 8)));
    } else if (bDate.length() == 10) {
        bCal.set(Integer.parseInt(bDate.substring(0, 4)), Integer.parseInt(bDate.substring(5, 7)) - 1,
                Integer.parseInt(bDate.substring(8, 10)));
    }

    return aCal.compareTo(bCal);
}

From source file:Main.java

public static boolean isPastDate(String year, String month, String day) {
    try {/*www .ja  v  a 2s  . c  o m*/
        GregorianCalendar cal = new GregorianCalendar();
        cal.setLenient(false);
        SimpleDateFormat fmDate = new SimpleDateFormat("yyyyMMdd");
        String today = fmDate.format(cal.getTime());
        cal.clear();
        cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
        String inputday = fmDate.format(cal.getTime());

        if (inputday.compareTo(today) < 0) {
            return true;
        } else {
            return false;
        }
    } catch (IllegalArgumentException e) {
        return false;
    }
}

From source file:Main.java

public static int getYears(int year, int month, int day) {

    GregorianCalendar cal = new GregorianCalendar();
    int y, m, d, noofyears;

    y = cal.get(Calendar.YEAR);// current year ,
    m = cal.get(Calendar.MONTH);// current month 
    d = cal.get(Calendar.DAY_OF_MONTH);//current day
    cal.set(year, month, day);// here ur date 
    noofyears = y - cal.get(Calendar.YEAR);
    if ((m < cal.get(Calendar.MONTH))
            || ((m == cal.get(Calendar.MONTH)) && (d < cal.get(Calendar.DAY_OF_MONTH)))) {
        --noofyears;/*from   www .  j a v a2 s  . c o m*/
    }
    try {
        if (noofyears < 0)
            throw new IllegalArgumentException("age < 0");
    } catch (IllegalArgumentException ile) {
        ile.printStackTrace();
    }
    System.out.println(noofyears);
    return noofyears;
}

From source file:Main.java

/**
 * Given a calendar (possibly containing only a day of the year), returns the earliest possible
 * anniversary of the date that is equal to or after the current point in time if the date
 * does not contain a year, or the date converted to the local time zone (if the date contains
 * a year.//from  w  ww  .  j a  v a  2  s  .  c  o m
 *
 * @param target The date we wish to convert(in the UTC time zone).
 * @return If date does not contain a year (year < 1900), returns the next earliest anniversary
 * that is after the current point in time (in the local time zone). Otherwise, returns the
 * adjusted Date in the local time zone.
 */
public static Date getNextAnnualDate(Calendar target) {
    final Calendar today = Calendar.getInstance();
    today.setTime(new Date());

    // Round the current time to the exact start of today so that when we compare
    // today against the target date, both dates are set to exactly 0000H.
    today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.MINUTE, 0);
    today.set(Calendar.SECOND, 0);
    today.set(Calendar.MILLISECOND, 0);

    final boolean isYearSet = isYearSet(target);
    final int targetYear = target.get(Calendar.YEAR);
    final int targetMonth = target.get(Calendar.MONTH);
    final int targetDay = target.get(Calendar.DAY_OF_MONTH);
    final boolean isFeb29 = (targetMonth == Calendar.FEBRUARY && targetDay == 29);
    final GregorianCalendar anniversary = new GregorianCalendar();
    // Convert from the UTC date to the local date. Set the year to today's year if the
    // there is no provided year (targetYear < 1900)
    anniversary.set(!isYearSet ? today.get(Calendar.YEAR) : targetYear, targetMonth, targetDay);
    // If the anniversary's date is before the start of today and there is no year set,
    // increment the year by 1 so that the returned date is always equal to or greater than
    // today. If the day is a leap year, keep going until we get the next leap year anniversary
    // Otherwise if there is already a year set, simply return the exact date.
    if (!isYearSet) {
        int anniversaryYear = today.get(Calendar.YEAR);
        if (anniversary.before(today) || (isFeb29 && !anniversary.isLeapYear(anniversaryYear))) {
            // If the target date is not Feb 29, then set the anniversary to the next year.
            // Otherwise, keep going until we find the next leap year (this is not guaranteed
            // to be in 4 years time).
            do {
                anniversaryYear += 1;
            } while (isFeb29 && !anniversary.isLeapYear(anniversaryYear));
            anniversary.set(anniversaryYear, targetMonth, targetDay);
        }
    }
    return anniversary.getTime();
}

From source file:ws.michalski.sepa.pain.SEPAUtility.java

/**
 * Konvertiert Datum als ISO int in GregorianCalendar
 * @param isoDate/*from w w  w  . jav a  2  s  .c  o m*/
 * @return
 */
public static GregorianCalendar convertYearOnly(int isoDate) {

    if (isoDate < 19990101) {
        isoDate = 19990101;
    }

    GregorianCalendar cal = new GregorianCalendar();

    String strDate = String.valueOf(isoDate);
    int yyyy = Integer.parseInt(strDate.substring(0, 4));
    int mm = Integer.parseInt(strDate.substring(4, 6));
    int dd = Integer.parseInt(strDate.substring(6, 8));

    cal.set(yyyy, mm - 1, dd);

    return cal;
}

From source file:test.unit.be.fedict.eid.idp.attribute.age.AgeAttributeServiceTest.java

@Test
public void testAge() throws Exception {
    // setup//w ww . ja v a 2s  . co m
    AgeAttributeService testedInstance = new AgeAttributeService();
    Map<String, Attribute> attributeMap = new HashMap<String, Attribute>();
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.set(1979, 0, 15);
    attributeMap.put(DefaultAttribute.DATE_OF_BIRTH.getUri(),
            new Attribute(DefaultAttribute.DATE_OF_BIRTH.getUri(), AttributeType.DATE, gregorianCalendar));

    // operate
    testedInstance.addAttribute(attributeMap);

    // verify
    Attribute ageAttribute = attributeMap.get("be:fedict:eid:idp:age");
    assertNotNull(ageAttribute);
    LOG.debug("age: " + ageAttribute.getValue());
}