"calendar date to yyyy-mm-dd format in java" Code Answer


a java date is a container for the number of milliseconds since january 1, 1970, 00:00:00 gmt.

when you use something like system.out.println(date), java uses date.tostring() to print the contents.

the only way to change 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 (or display).

java 8+

localdatetime ldt = localdatetime.now().plusdays(1);
datetimeformatter formmat1 = datetimeformatter.ofpattern("yyyy-mm-dd", locale.english);
// output "2018-05-12t17:21:53.658"

string formatter = formmat1.format(ldt);
// 2018-05-12

prior to java 8

you should be making use of the threeten backport

the following is maintained for historical purposes (as the original answer)

what you can do, is format the date.

calendar cal = calendar.getinstance();
cal.add(calendar.date, 1);
simpledateformat format1 = new simpledateformat("yyyy-mm-dd");
// output "wed sep 26 14:23:28 est 2012"

string formatted = format1.format(cal.gettime());
// output "2012-09-26"

// output "wed sep 26 00:00:00 est 2012"

these are actually the same date, represented differently.

By fuzzyLikeSheep on February 2 2022

Answers related to “calendar date to yyyy-mm-dd format in java”

Only authorized users can answer the Search term. Please sign in first, or register a free account.