[英]How to print the date in dd/mm format in java
I want to write a class so that I can print the date in dd/mm
format, but I have no idea to start. 我想编写一个类,以便可以以dd/mm
格式打印日期,但是我不知道要开始。
Here is the main()
part 这是main()
部分
Code: 码:
import java.util.Arrays;
public class SortMonthDate {
public static void main(String[] args) {
MonthDate[] mdates = new MonthDate[5];
mdates[0] = new MonthDate(22, 7);
mdates[1] = new MonthDate(25, 8);
mdates[2] = new MonthDate(25, 6);
mdates[3] = new MonthDate(28, 9);
mdates[4] = new MonthDate(5, 9);
print(mdates);
Arrays.sort(mdates);
print(mdates);
}
static <T> void print(T[] a) {
for (T t : a) {
System.out.printf("%s ", t);
}
System.out.println();
}
}
There's a class for that! 有一个课程!
java.time.MonthDay
Rather than roll-your-own, use the MonthDay
class built into Java 8 and later. 而不是自己动手,而是使用Java 8和更高版本中内置的MonthDay
类。 For Java 6 & 7, use the back-port. 对于Java 6和7,请使用后端口。
MonthDay[] mds = new MonthDay[5] ;
mds[0] = MonthDay.of( 7 , 22 ) ;
mds[1] = MonthDay.of( Month.AUGUST , 25 ) ;
mds[2] = MonthDay.of( Month.JUNE , 25 ) ;
mds[3] = MonthDay.of( Month.SEPTEMBER , 28 );
mds[4] = MonthDay.of( 9 , 5 ) ;
Arrays.sort(mdates);
Better to use Java Collections generally. 通常最好使用Java集合 。
List< MonthDay > mds = new ArrayList<>() ;
mds.add( MonthDay.of( 7 , 22 ) ) ;
mds.add( MonthDay.of( Month.AUGUST , 25 ) ) ;
mds.add( MonthDay.of( Month.JUNE , 25 ) ) ;
mds.add( MonthDay.of( Month.SEPTEMBER , 28 ) ) ;
mds.add( MonthDay.of( 9 , 5 ) ) ;
Collections.sort( mds ) ;
I want to write a class so that I can print the date in dd/mm format, 我想编写一个类,以便可以以dd / mm格式打印日期,
To learn about writing the class, check out the source code in the OpenJDK project. 要了解有关编写类的信息,请查看OpenJDK项目中的源代码 。
As for generating text representing that month-day class, simply call toString
to generate a value in standard ISO 8601 format. 至于生成表示该月日班级的文本,只需调用toString
即可生成标准ISO 8601格式的值。 I strongly suggest use these standard formats for logging, storing, and exchanging date-time values as text. 我强烈建议使用这些标准格式记录,存储和交换日期时间值作为文本。
MonthDay.of( Month.JUNE , 25 ) )
.toString()
--06-25 --06-25
For presentation to the user, specify your desired format with DateTimeFormatter
class. 为了向用户展示,请使用DateTimeFormatter
类指定所需的格式。 Search Stack Overflow for more info, as this has been covered many many times already. 搜索堆栈溢出以获取更多信息,因为已经讨论了很多次。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM" ) ;
String output = mds.get( 0 ).format( f ) ; // Generate string in custom format.
25/06 25/06
Dump to console. 转储到控制台。
System.out.println( "mds: " + mds ) ;
System.out.println( "mds.get( 0 ) = " + mds.get( 0 ) + " | output: " + output ) ;
See this code run live at IdeOne.com . 看到此代码在IdeOne.com上实时运行 。
mds: [--06-25, --07-22, --08-25, --09-05, --09-28] mds:[-06-25,-07-22,-08-25,-09-05,-09-28]
mds.get( 0 ) = --06-25 | mds.get(0)= --06-25 | output: 25/06 输出:25/06
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.Date
, Calendar
和SimpleDateFormat
。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 。 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310 。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 。 No need for strings, no need for java.sql.*
classes. 不需要字符串,不需要java.sql.*
类。
Where to obtain the java.time classes? 在哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多 。
If you are printing an Object to System.out
Java uses the Object.toString() method to get a string representation of your Object. 如果要将对象打印到System.out
Java使用Object.toString()方法来获取对象的字符串表示形式。 So you can overwrite the default toString()
method by implementing it in your MonthDate
class. 因此,您可以通过在MonthDate
类中实现默认的toString()
方法来覆盖它。
To format the int values you can use the String.format() method. 要格式化int值,可以使用String.format()方法。 The format %02d
pads the value with a 0
if smaller than 10. To get the dd/mm
format you can use %02d/%02d
. 如果小于10,则格式%02d
用0
%02d
值。要获取dd/mm
格式,可以使用%02d/%02d
。
With that said your toString()
method can look like this: 这样说,您的toString()
方法可以如下所示:
@Override
public String toString() {
return String.format("%02d/%02d", this.day, this.month);
}
Using new MonthDate(12, 3)
this would print: 使用new MonthDate(12, 3)
将打印:
12/03
Instead of: 代替:
MonthDate@42af3438
Assuming that your MonthDate
class attributes are 假设您的MonthDate
类属性是
private int day;
private int month;
You should always implement a toString()
method for every class in java because you will be representing every object differently. 您应该始终为Java中的每个类实现toString()
方法,因为您将以不同的方式表示每个对象。
As for sorting your objects by date, you cannot use Arrays.sort()
because you're not sorting numbers. 至于按日期对对象进行排序,则不能使用Arrays.sort()
因为您没有对数字进行排序。 You should implement your own sorting method.Here's one i recommend you: 您应该实现自己的排序方法。以下是我推荐的一种方法:
public static void sort(MonthDate[] s) {
int[] days = new int[s.length];
int[] months = new int[s.length];
for (int i = 0; i < s.length; i++) {
days[i] = s[i].day;
months[i] = s[i].month;
}
Arrays.sort(days);
Arrays.sort(months);
for (int i = 0; i < s.length; i++) {
s[i].day = days[i];
s[i].month = months[i];
}
}
We put the days
and months
in 2 different arrays and sorted them using Arrays.sort()
, then assigned them back to the object instance. 我们将days
和months
放入2个不同的数组中,并使用Arrays.sort()
对其进行Arrays.sort()
,然后将其分配回对象实例。
You can also implement this toString()
method and then call it in your print()
method, because you cannot print an object directly with System.out.println()
. 您还可以实现此toString()
方法,然后在您的print()
方法中调用它,因为您不能直接使用System.out.println()
打印对象。
public String toString() {
return "" + this.day + "/" + this.month;
}
In your print()
function you should use the toString()
method as so: 在您的print()
函数中,您应该这样使用toString()
方法:
public static <T> void print(T[] a) {
for (T t : a) {
System.out.println(t.toString());
}
System.out.println();
}
Finally, you can keep your main
like that and it would sort it and print it correcty. 最后,您可以像这样保留您的main
,它将对其进行排序并正确打印。
public static void main(String[] args) {
MonthDate[] mdates = new MonthDate[5];
mdates[0] = new SortMonthDate(22, 7);
mdates[1] = new SortMonthDate(25, 8);
mdates[2] = new SortMonthDate(25, 6);
mdates[3] = new SortMonthDate(28, 9);
mdates[4] = new SortMonthDate(5, 9);
print(mdates);
sort(mdates);
print(mdates);
}
Here's the full code: 这是完整的代码:
MonthDate class MonthDate班
import java.util.Arrays;
public class MonthDate {
private int day;
private int month;
public MonthDate(int day, int month) {
this.day = day;
this.month = month;
}
public static void sort(MonthDate[] s) {
int[] days = new int[s.length];
int[] months = new int[s.length];
for (int i = 0; i < s.length; i++) {
days[i] = s[i].day;
months[i] = s[i].month;
}
Arrays.sort(days);
Arrays.sort(months);
for (int i = 0; i < s.length; i++) {
s[i].day = days[i];
s[i].month = months[i];
}
}
public String toString() {
return "" + this.day + "/" + this.month;
}
public static <T> void print(T[] a) {
for (T t : a) {
System.out.println(t.toString());
}
System.out.println();
}
}
SortMonthDate class SortMonthDate类
public class SortMonthDate {
public static void main(String[] args) {
MonthDate[] mdates = new MonthDate[5];
mdates[0] = new MonthDate(22, 7);
mdates[1] = new MonthDate(25, 8);
mdates[2] = new MonthDate(25, 6);
mdates[3] = new MonthDate(28, 9);
mdates[4] = new MonthDate(5, 9);
MonthDate.print(mdates);
MonthDate.sort(mdates);
MonthDate.print(mdates);
}
}
Note that instead of making your MonthDate
methods static, you could make them not static and call them in main
using, for example, mdates.print();
注意,除了使您的MonthDate
方法静态化之外,还可以使它们不是静态的,并使用例如mdates.print();
在main
调用它们mdates.print();
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.