GregorianCalendar Class in Java

Ahh, the joys of the Java Date/Time API … What you want (aside from a better API, such as Joda Time) is a DateFormat. It can print dates in a time zone you specify. You don’t need Calendar for that. dateFormat.setTimeZone(TimeZone.getTimeZone(“Asia/Bangkok”)); dateFormat.format(new Date()); Calendar is for time manipulations and calculations. For example “set the time … Read more

converting gregorian to hijri date

Firstly, separate out the conversion part from the formatting/parsing part. You can deal with those easily later – and there are lots of questions on Stack Overflow about that. Personally I’d use Joda Time, which typically makes life much simpler. For example: import org.joda.time.Chronology; import org.joda.time.LocalDate; import org.joda.time.chrono.IslamicChronology; import org.joda.time.chrono.ISOChronology; public class Test { public … Read more

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.