Calendar date to yyyy-MM-dd format in java

Calendar date to yyyy-MM-dd format in java

Asked on December 12, 2018 in Java.
Add Comment


  • 5 Answer(s)

    Please read the approach given below.
    The Date keyword or function in the java consist of the milliseconds since the January 1, 1970, 00:00:00 GMT.

    If you want to print time or date use System.out.println(date). For contents Date.toString()

    Here, the easy way to convert  it is to override Date and provide your own implementation of Date.toString(). Now before you fire up your IDE and try this, I wouldn’t; it will only complicate matters. You are better off formatting the date to the format you want to use.

    Java 8+:

    LocalDateTime ldt = LocalDateTime.now().plusDays(1);
    DateTimeFormatter formmat1 = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
    System.out.println(ldt);
    // Output "2018-05-12T17:21:53.658"
     
    String formatter = formmat1.format(ldt);
    System.out.println(formatter);
    // 2018-05-12
    

    Precedence to java 8:

    Maintained for historical purposes you should make the use of  ThreeTen Backport.

    This one is for formatting the date,

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1);
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(cal.getTime());
    // Output "Wed Sep 26 14:23:28 EST 2012"
     
    String formatted = format1.format(cal.getTime());
    System.out.println(formatted);
    // Output "2012-09-26"
     
    System.out.println(format1.parse(formatted));
    // Output "Wed Sep 26 00:00:00 EST 2012"
    
    Answered on December 12, 2018.
    Add Comment

    The solution for the given query is,

    When you want to display it, you can format the calender date object and  keep that as a string.

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1);
    Date date = cal.getTime();
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    String inActiveDate = null;
    try {
        inActiveDate = format1.format(date);
        System.out.println(inActiveDate );
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    
    Answered on December 12, 2018.
    Add Comment

    use the code given below,

    Instead of using java.util.date  you have to use SimpleDateFormat.format method that returns string. 

    String myString=format1.format(date);
    
    Answered on December 12, 2018.
    Add Comment

    java.util.Date object can’t represent date in custom format instead you’ve to use SimpleDateFormat.format method that returns string.

    String myString=format1.format(date);
    Answered on January 18, 2019.
    Add Comment

    convert  it is to override Date and provide your own implementation ofDate.toString(). Now before you fire up your IDE and try this, I wouldn’t; it will only complicate matters. You are better off formatting the date to the format you want to use.

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 7);
    Date date = c.getTime();
    SimpleDateFormat ft = new SimpleDateFormat("MM-dd-YYYY");
    JOptionPane.showMessageDialog(null, ft.format(date));
    Answered on January 18, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.