Sunday 12 April 2020

Convert Calendar to yyyy-MM-dd format

Using 'SimpleDateFormat', you can convert Calendar instance to 'yyyy-MM-dd' format.

Example
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String date = simpleDateFormat.format(cal.getTime());

App.java
package com.sample.app;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class App {

 public static void main(String args[]) {
  Calendar cal = Calendar.getInstance();

  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

  String date = simpleDateFormat.format(cal.getTime());

  System.out.println("date : " + date);
  
 }
}

Output
date : 2020-01-25

You may like

No comments:

Post a Comment