Java Convert Calendar to String
Tags: Java Calendar String
In this Java core tutorial we learn how to convert a java.util.Calendar object to a date time format String in Java programming language.
How to format Calendar to date time String
In Java we can use SimpleDateFormat class to format a Date object into date time String. The following Java program we use the SimpleDateFormat to format Date object which return from Calendar.getTime() method of a given Calendar object.
ConvertCalendarToStringExample1.java
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ConvertCalendarToStringExample1 {
public static void main(String... args) {
Calendar calendar = Calendar.getInstance();
// Convert Calendar to String
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String formattedDate = simpleDateFormat.format(calendar.getTime());
System.out.println(formattedDate);
}
}
2022/04/16 13:59:42
Happy Coding 😊
Related Articles
Java Convert Calendar to Milliseconds