Displaying date in a double digit format
Solution 1
DecimalFormat mFormat= new DecimalFormat("00");
mFormat.format(Double.valueOf(year));
in your case:
mFormat.setRoundingMode(RoundingMode.DOWN);
String Dates = mFormat.format(Double.valueOf(year)) + "-" + mFormat.format(Double.valueOf(Place)) + "-" + mFormat.format(Double.valueOf(day));
Solution 2
Please use SimpleDateFormat
SimpleDateFormat sd1 = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println("Date : " + sd1.format(new Date(c.getTimeInMillis()));
Output
Date : 18-Apr-2012
Solution 3
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH)+1;
String Dates = year + "-" +(month<10?("0"+month):(month)) + "-" + day;
Date.setText((Dates));
Solution 4
if (dayOfMonth < 10) {
NumberFormat f = new DecimalFormat("00");
Sting date = String.valueOf(f.format(dayOfMonth));
}
Solution 5
tl;dr
LocalDate.now()
.toString()
2016-01-07
Better to always specify your desired/expected time zone explicitly.
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.toString()
Avoid old date-time classes
The java.util.Calendar class you are using is now legacy. Avoid this class, along with java.util.Date and such. These have proven to be poorly designed, confusing, and troublesome.
java.time
The old date-time classes have been supplanted by the java.time framework built into Java 8 and later. Much of the functionality has been back-ported to Java 6 & 7 in the ThreeTen-Backport project, and further adapted to Android in ThreeTenABP.
These modern classes can accomplish your goal in a single line of code, as seen below.
Date-time formatting features
Both the old outmoded date-time classes as well as java.time classes offer formatting features. No need for you to be writing your own formatting code.
Time Zone
Time zone is crucial to determine the date. The date varies around the world by time zone as a new day dawns earlier in the east. This issue was ignored in the Question and other Answers. If omitted, the JVM’s current default time zone is implicitly applied. Better to explicitly specify your desired/expected time zone.
LocalDate
For a date-only value without time-of-day and without time zone, use the LocalDate
. While no time zone is stored within the LocalDate
, a time zone determines “today”.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
ISO 8601
Your desired format of YYYY-MM-DD with double digits happens to comply with the ISO 8601 standard defining sensible formats for textual representations of date-time values. This standard is used by default in java.time classes for parsing/generating strings.
String output = today.toString();
That’s all, 3 lines of code. You could even combine them.
String output = LocalDate.now( ZoneId.of( "America/Montreal" ) ).toString();
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
-
Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Comments
-
The Tokenizer about 4 years
I am having trouble displaying the date in a double digit format. I want it to be so that when the day or the month is a single digit example: 4 It would display 04. I'm having trouble coming up with the logic for it, if somebody can help me I would be really grateful.
Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int day = c.get(Calendar.DAY_OF_MONTH); int month = c.get(Calendar.MONTH); if (month % 10 == 0) { Place = 0 + month; } String Dates = year + "-" + Place + "-" + day; Date.setText((Dates));