Get current date in YYYY-MM-DD format in Java (Android)

18,864

Change this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");

to this:

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

mm stands for the minutes, while MM (capitalized) stands for the month

Share:
18,864

Related videos on Youtube

petehallw
Author by

petehallw

Taught myself Java with the Swing framework and Android development after some OOP experience with C++. I'm currently learning design patterns and web development technologies such as HTML, CSS and JavaScript/JQuery/AJAX.

Updated on June 07, 2022

Comments

  • petehallw
    petehallw almost 2 years

    I am trying to get a Date object in the format YYYY-MM-DD representing the current system date. Below is my attempt:

    Date todayDate = Calendar.getInstance().getTime();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
    String todayString = formatter.format(todayDate);
    

    The values are as follows:

    todayDate: Mon May 15 16:24:47 GMT+01:00 2017

    todayString: 2017-24-15

    Having tried this a couple of times I noticed the todayString is not made up of YYYY-MM-DD but YYYY-[minutes][minutes]-DD.

    How might I get the current date in the YYYY-MM-DD format?

    • petehallw
      petehallw about 7 years
      @OleV.V. I think it's crazy that this functionality has not been present in Java before version 8... Thanks for the link, I'm reading through it!
  • Basil Bourque
    Basil Bourque about 7 years
    Answering an obviously duplicate Question is not helpful.