English 中文(简体)
以两位数格式显示日期
原标题:Displaying date in a double digit format

我正在以两位数格式展示这个日期。 我想这样说,当日或当月是一位数字例子时: 4 显示4个。 如果有人能帮助我,我会真的感激,那么我会抱着这一逻辑。

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));
最佳回答
DecimalFormat mFormat= new DecimalFormat("00");
mFormat.format(Double.valueOf(year));

阁下:

 mFormat.setRoundingMode(RoundingMode.DOWN);
 String Dates =  mFormat.format(Double.valueOf(year)) + "-" +  mFormat.format(Double.valueOf(Place)) + "-" +  mFormat.format(Double.valueOf(day));
问题回答

请使用简单的表格

SimpleDateFormat sd1 = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println("Date : " + sd1.format(new Date(c.getTimeInMillis()));

产出

Date : 18-Apr-2012
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));
if (dayOfMonth < 10) {
    NumberFormat f = new DecimalFormat("00");
    Sting date = String.valueOf(f.format(dayOfMonth));
}

您可以使用“Sting.format()”方法。

例如:

String.format("%02d", month);

如果选择的月份少于10个月,则在月前增加“0”。

tl;dr

LocalDate.now()
         .toString()

2016-01-07

最好总是明确规定你所期望/期望的时间区。

LocalDate.now( ZoneId.of( "America/Montreal" ) )
         .toString()

Avoid old date-time classes

The java.util. 现在,你使用的日历级是遗产。 避免这一类以及java。 日期和日期。 事实证明,这些问题设计不好,混淆不清,麻烦多。

java.time

旧的日间班由java.time 。 框架载于 Java8和随后。 大部分功能已退回到Java 6 &7功能载于。 三个Ten-Backport项目,并进一步适应。 3TenABP

这些现代班子可以在一条单一的法典线上实现你的目标,如下文所示。

Date-time formatting features

旧过时的学期班和 j。 时间班提供格式特征。 无需你写自己的格式编码。

Time Zone

时间区对于确定日期至关重要。 随着东部新时代的到来,这个日期在世界各地因时区而异。 这个问题在问题和其他答复中被忽视。 如果省略,科索沃信托局目前的违约时区就暗中适用。 更明确地规定你所期望/预期的时间区。

LocalDate

如无时日和无时区,则使用。 虽然没有将任何时间区储存在<代码>当地<><>>内,但时间区则确定为“日常”。

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();

这完全是三条法典。 你们甚至可以把他们结合起来。

String output = LocalDate.now( ZoneId.of( "America/Montreal" ) ).toString();

About java.time

java.time Framework is into Java 8 and subsequently. 这些班级取代麻烦的旧的legacyjava.util。 日期<>,rel=“nofollow noretinger”>Calendar >, &

Joda-time< Project,现在载于mainance Mode,建议向以下人员迁移:a href=“http://docs.ora.com/javase/9/docsa/pi/java/parm/sumer_mage. 班级。

欲了解更多情况,见 rel=“nofollow noreferer”<>>。 此外,还寻找Stack Overflow的许多实例和解释。 具体内容是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.

获取ava。 时间段

>ThreeTen-Extra 项目延长java.time 额外课程。 该项目是今后可能增加 j的一个证明。 时间。 http://www. 33ten.org/ iii/extra/apidocs/org/ 3ten/extra/Interval.html

肯定使用 Java 8 时间图书馆:

String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
if ((month+1)<10){
    place = "0"+(String) (month+1)
}

每天也这样做,你是好的。

月+1,因为它从零开始。

Joda-Time

利用Joda-time Library,特别是 < 班级。

DateTime datetime = new DateTime(new Date());
String month = datetime.toString("MM");

结果是2位数

If you need for example a year you can use:

String year = datetime.toString("YYYY");

result will be 4 digits of the year.

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()
            
        }

www.un.org/spanish/ecosoc 但是,你只需要这一部分。

 val mFormat = DecimalFormat("00")
 mFormat.roundingMode = RoundingMode.DOWN
 val date: String = mFormat.format((selectedyear)) + "-" + mFormat.format((selectedmonth)) + "-" + mFormat.format((selectedday))
  binding.edtDob.setText(date)




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签