Convert a string to a GregorianCalendar

Use SimpleDateFormat to parse the date and then assign it to a Calendar. DateFormat df = new SimpleDateFormat(“dd MM yyyy”); Date date = df.parse(“02 26 1991”); Calendar cal = Calendar.getInstance(); cal.setTime(date); The third line could be replaced with: Calendar cal = new GregorianCalendar(); but I prefer the first version.

Using GregorianCalendar with SimpleDateFormat

SimpleDateFormat.format() method takes a Date as a parameter. You can get a Date from a Calendar by calling its getTime() method: public static String format(GregorianCalendar calendar) { SimpleDateFormat fmt = new SimpleDateFormat(“dd-MMM-yyyy”); fmt.setCalendar(calendar); String dateFormatted = fmt.format(calendar.getTime()); return dateFormatted; } Also note that the months start at 0, so you probably meant: int month = … Read more

Getting current datetime using Calendar.getInstance() vs new GregorianCalendar()

Looking in the source of Calendar.getInstance(): private static Calendar createCalendar(TimeZone zone, Locale aLocale) { // If the specified locale is a Thai locale, returns a BuddhistCalendar // instance. if (“th”.equals(aLocale.getLanguage()) && (“TH”.equals(aLocale.getCountry()))) { return new sun.util.BuddhistCalendar(zone, aLocale); } else if (“JP”.equals(aLocale.getVariant()) && “JP”.equals(aLocale.getCountry()) && “ja”.equals(aLocale.getLanguage())) { return new JapaneseImperialCalendar(zone, aLocale); } // else create the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)