Convert a String to a Calendar object in Java
Codemia Knowledge Hub ·
To convert a date string to a Calendar in Java, parse it into a Date with SimpleDateFormat, then create a Calendar with Calendar.getInstance() and assign the date with setTime(). On Java 8 and later, the java.time API offers cleaner alternatives.
Calendar and SimpleDateFormat
Calendar, in the java.util package, is an abstract, flexible class with methods for converting between a specific instant and date and time fields such as YEAR, MONTH, DAY_OF_MONTH and HOUR. It can also compute a date and time from milliseconds.
SimpleDateFormat lets you define a pattern that matches the format of your string. A basic example:
How the conversion works
- Define the string and format: the string
"2023-10-14 15:30:00"is paired with aSimpleDateFormatusing the pattern"yyyy-MM-dd HH:mm:ss". - Parse the string to a
Date:parse()converts the string to aDate. It throws aParseExceptionif the string does not match the pattern, so it is wrapped in a try-catch block. - Convert
DatetoCalendar:Calendar.getInstance()returns aCalendar, andsetTime()assigns theDateto it.
Handling ParseException
ParseException is thrown when the date string does not match the specified format. Make sure the pattern lines up with the string format.
Thread safety, locale and time zone
- Thread safety:
SimpleDateFormatis not thread-safe. In multi-threaded code, use a separate instance per thread or synchronize access. - Locale and time zone:
SimpleDateFormatcan be initialized with aLocaleandTimeZone, which matters for applications that handle dates across locales or time zones.
Date vs Calendar
| Aspect | Date Class | Calendar Class |
| Mutability | Mutable | Mutable |
| Representation | Represents a specific instant in time | Represents a specific moment in time with fields like YEAR, MONTH, etc. |
| Manipulation | Limited date manipulation | Rich set of utilities for date manipulation |
| Deprecation | Many of its methods are deprecated | Not deprecated and encouraged for date manipulations |
The java.time alternative
SimpleDateFormat and Calendar are enough for many tasks, but the Java 8 Date-Time API in the java.time package provides more modern, intuitive classes such as LocalDateTime and ZonedDateTime that solve many of the problems with Calendar and Date:
Both approaches have their uses. Choose based on your application's requirements and the Java version you target.
Related reading
- Convert String to double in Java
- Converting a Date object to a calendar object
- Converting a date string to a DateTime object using Joda Time library
- Converting a string to an integer on Android
- Converting an int to a binary string representation in Java?
- Converting array to list in Java
- Converting ''ArrayList<String> to ''String[]'' in Java
- Converting between java.time.LocalDateTime and java.util.Date
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.