Convert Calendar date to yyyy-MM-dd format in java

Java

How to convert calendar date to yyyy-MM-dd format.

Calendar cal = Calendar.getInstance();

cal.add(Calendar.DATE, 1);

Date date = cal.getTime();             

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

String date1 = format1.format(date);            

Date inActiveDate = null;

try {

    inActiveDate = format1.parse(date1);

} catch (ParseException e1) {

    // TODO Auto-generated catch block

    e1.printStackTrace();

}

 

This will produce in Active Date = Wed Sep 26 00:00:00 IST 2012. But what I need is 2012-09-26. My purpose is to compare this date with another date in my database using Hibernate criteria. So I need the date object in yyyy-MM-dd format.


If you want to unleash your potential in this competitive field, please visit the Java course page for more information, where you can find the Java tutorials and Java frequently asked interview questions and answers as well.

2
Answers

Replies

You will need to right click on your date field, you'll get a menu with the following options:


"Filter" , "Show quick filter", etc. You will also see an option which says "Day.... May 8, 2011". That gives you a full date.


But if you want that date to have the "mm/dd/yyyy" format, you'll need to change it from the left pane. When you right click on your date dimension provides the following option: "Default Properties/Date Format". You have a few default formats in there, including a custom formatting where you could make it mm/dd/yyyy.

 

we can convert one date format into another date format performing a few steps. we can simply pass the changed date format “yyyy-MM-dd” in SimpleDateFormat constructor and call format() method with SimpleDateFormat object and it returns the formatted date as a String.


Whatever code you have written for the conversion of one date format into the desired date format is absolutely correct but you don’t need to parse the formatted date by using the statement “format1.parse(date1)”.


We are going to share the customized version of your own code are given below:


Customized Code:


import java.util.*;

import java.text.*;

public class DateFormat {

    public static void main(String args[]) {

      Calendar cal = Calendar.getInstance();

      cal.add(Calendar.DATE, 1);


      

      Date date = cal.getTime();             

      //System.out.println(date);


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

      

      // To format date into the given format

      // "yyyy-MM-dd"

      String date1 = format1.format(date);            

      

      String inActiveDate = null;   

      try {


            inActiveDate = date1;

            System.out.println(inActiveDate);


          } catch (Exception e1) {


                // TODO Auto-generated catch block


                 e1.printStackTrace();


            }

    }

}

 


In the above code, we have changed three things


First, we wrote “String inActiveDate” instead of “Date inActiveDate ” because it holds the value of date1 and date1 is of “String” type.
Second, we wrote “inActiveDate = date1” instead of “format1.parse(date1)” because there is no need of parsing the formatted date. we can simply assign the formatted date.
Third, we wrote “catch(Exception e1)” instead of “catch(ParseException e1)” because we haven’t used a parse statement so it will not throw ParseException.


 

 
 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.
WhatsApp
To Top