![](/img/trans.png)
[英]How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java
[英]How to print the date in dd/mm format in java
我想编写一个类,以便可以以dd/mm
格式打印日期,但是我不知道要开始。
这是main()
部分
码:
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();
}
}
有一个课程!
java.time.MonthDay
而不是自己动手,而是使用Java 8和更高版本中内置的MonthDay
类。 对于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);
通常最好使用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 ) ;
我想编写一个类,以便可以以dd / mm格式打印日期,
要了解有关编写类的信息,请查看OpenJDK项目中的源代码 。
至于生成表示该月日班级的文本,只需调用toString
即可生成标准ISO 8601格式的值。 我强烈建议使用这些标准格式记录,存储和交换日期时间值作为文本。
MonthDay.of( Month.JUNE , 25 ) )
.toString()
--06-25
为了向用户展示,请使用DateTimeFormatter
类指定所需的格式。 搜索堆栈溢出以获取更多信息,因为已经讨论了很多次。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM" ) ;
String output = mds.get( 0 ).format( f ) ; // Generate string in custom format.
25/06
转储到控制台。
System.out.println( "mds: " + mds ) ;
System.out.println( "mds.get( 0 ) = " + mds.get( 0 ) + " | output: " + output ) ;
看到此代码在IdeOne.com上实时运行 。
mds:[-06-25,-07-22,-08-25,-09-05,-09-28]
mds.get(0)= --06-25 | 输出:25/06
java.time框架内置于Java 8及更高版本中。 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.Date
, Calendar
和SimpleDateFormat
。
要了解更多信息,请参见Oracle教程 。 并在Stack Overflow中搜索许多示例和说明。 规格为JSR 310 。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
您可以直接与数据库交换java.time对象。 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 。 不需要字符串,不需要java.sql.*
类。
在哪里获取java.time类?
ThreeTen-Extra项目使用其他类扩展了java.time。 该项目为将来可能在java.time中添加内容提供了一个试验场。 您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多 。
如果要将对象打印到System.out
Java使用Object.toString()方法来获取对象的字符串表示形式。 因此,您可以通过在MonthDate
类中实现默认的toString()
方法来覆盖它。
要格式化int值,可以使用String.format()方法。 如果小于10,则格式%02d
用0
%02d
值。要获取dd/mm
格式,可以使用%02d/%02d
。
这样说,您的toString()
方法可以如下所示:
@Override
public String toString() {
return String.format("%02d/%02d", this.day, this.month);
}
使用new MonthDate(12, 3)
将打印:
12/03
代替:
MonthDate@42af3438
假设您的MonthDate
类属性是
private int day;
private int month;
您应该始终为Java中的每个类实现toString()
方法,因为您将以不同的方式表示每个对象。
至于按日期对对象进行排序,则不能使用Arrays.sort()
因为您没有对数字进行排序。 您应该实现自己的排序方法。以下是我推荐的一种方法:
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];
}
}
我们将days
和months
放入2个不同的数组中,并使用Arrays.sort()
对其进行Arrays.sort()
,然后将其分配回对象实例。
您还可以实现此toString()
方法,然后在您的print()
方法中调用它,因为您不能直接使用System.out.println()
打印对象。
public String toString() {
return "" + this.day + "/" + this.month;
}
在您的print()
函数中,您应该这样使用toString()
方法:
public static <T> void print(T[] a) {
for (T t : a) {
System.out.println(t.toString());
}
System.out.println();
}
最后,您可以像这样保留您的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);
}
这是完整的代码:
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类
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);
}
}
注意,除了使您的MonthDate
方法静态化之外,还可以使它们不是静态的,并使用例如mdates.print();
在main
调用它们mdates.print();
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.