Reputation: 1564
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));
Upvotes: 26
Views: 75418
Reputation: 598
binding.datePicker.setOnClickListener {
val mCurrentDate: Calendar = Calendar.getInstance()
val mYear: Int = mCurrentDate.get(Calendar.YEAR)
val mMonth: Int = mCurrentDate.get(Calendar.MONTH)
val mDay: Int = mCurrentDate.get(Calendar.DAY_OF_MONTH)
val mDatePicker = DatePickerDialog(
requireActivity(), R.style.DialogTheme,
{ datepicker, selectedyear, selectedmonth, selectedday ->
val selectedmonth = selectedmonth + 1
val mFormat = DecimalFormat("00")
mFormat.roundingMode = RoundingMode.DOWN
val date: String =
mFormat.format((selectedyear)) + "-" + mFormat.format((selectedmonth)
) + "-" + mFormat.format((selectedday))
binding.edtDob.setText(date)
}, mYear - 18, mMonth, mDay - 1
)
// set maximum date to be selected as today
// age more then 18+
mCurrentDate.add(Calendar.YEAR, -18);
mCurrentDate.add(Calendar.DAY_OF_MONTH, -1)
mDatePicker.datePicker.maxDate = mCurrentDate.timeInMillis
mDatePicker.show()
}
However, you only require this part.
val mFormat = DecimalFormat("00")
mFormat.roundingMode = RoundingMode.DOWN
val date: String = mFormat.format((selectedyear)) + "-" + mFormat.format((selectedmonth)) + "-" + mFormat.format((selectedday))
binding.edtDob.setText(date)
Upvotes: 0
Reputation: 1420
Purely using the Java 8 Time library:
String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Upvotes: 5
Reputation: 339332
LocalDate.now()
.toString()
2016-01-07
Better to always specify your desired/expected time zone explicitly.
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.toString()
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.
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.
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 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 );
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();
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?
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.
Upvotes: 5
Reputation: 105
you can use String.format() method.
example :
String.format("%02d", month);
so they would add "0" in front of month if the chosen month less than 10.
Upvotes: 7
Reputation: 1787
You can use SimpleDateFormat class
String dates = new java.text.SimpleDateFormat("dd-MM-YYYY").format(new Date())
for more information https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Upvotes: 2
Reputation: 1329
Using the Joda-Time library, specifically the org.joda.time.DateTime
class.
DateTime datetime = new DateTime(new Date());
String month = datetime.toString("MM");
result will be 2 digits
If you need for example a year you can use:
String year = datetime.toString("YYYY");
result will be 4 digits of the year.
Upvotes: 0
Reputation: 2028
if (dayOfMonth < 10) {
NumberFormat f = new DecimalFormat("00");
Sting date = String.valueOf(f.format(dayOfMonth));
}
Upvotes: 8
Reputation: 42016
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));
Upvotes: 54
Reputation: 13501
Use SimpleDateFormat class.. There are a lot of ways to do it..
something like this..
SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yy"); // you can add any format..
Date date = sdfSource.parse(strDate);
Upvotes: 2
Reputation: 9590
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));
Upvotes: 12
Reputation: 437
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
Upvotes: 13
Reputation: 3121
if ((month+1)<10){
place = "0"+(String) (month+1)
}
do the same for day and you are good to go.
+1 in month because it starts with 0.
Upvotes: 3