如何在java中以dd/mm格式打印日期

如何在java中以dd/mm格式打印日期,java,datetime-format,Java,Datetime Format,我想写一个类,这样我就可以用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);

我想写一个类,这样我就可以用
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.util.array;
公共类SortMonthDate{
公共静态void main(字符串[]args){
月日[]月日=新月日[5];
mdates[0]=新的MonthDate(22,7);
mdate[1]=新的MonthDate(25,8);
mdate[2]=新的MonthDate(25,6);
mdate[3]=新的MonthDate(28,9);
mdate[4]=新的MonthDate(5,9);
打印(mdates);
数组。排序(mdates);
打印(mdates);
}
静态无效打印(T[]a){
对于(T:a){
System.out.printf(“%s”,t);
}
System.out.println();
}
}

如果要将对象打印到
System.out
Java使用该方法获取对象的字符串表示形式。因此,您可以通过在
MonthDate
类中实现来覆盖默认的
toString()
方法

要格式化int值,可以使用以下方法。如果小于10,则格式
%02d
0
填充值。要获取
dd/mm
格式,可以使用
%02d/%02d

因此,您的
toString()
方法可以如下所示:

@Override
public String toString() {
    return String.format("%02d/%02d", this.day, this.month);
}
使用
newmonthDate(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];
    }
}
print()
函数中,应使用
toString()
方法,如下所示:

public static <T> void print(T[] a) {
    for (T t : a) {
        System.out.println(t.toString());
    }
    System.out.println();
}
以下是完整的代码:

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

}
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
方法设置为非静态,并在
main
中调用它们,例如使用
mdates.print()

有一个这样的课程

java.time.MonthDay
使用内置于Java8和更高版本中的
MonthDay
类,而不是使用自己的类。对于Java6和Java7,使用后端口

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);
最好是普遍使用

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 ) ;
--06-25

要向用户演示,请使用类指定所需的格式。搜索堆栈溢出以获取更多信息,因为这已经被讨论过很多次了

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM" ) ;
String output = mds.get( 0 ).format( f ) ;  // Generate string in custom format.
2006年5月25日

转储到控制台

System.out.println( "mds: " + mds ) ;
System.out.println( "mds.get( 0 ) = " +  mds.get( 0 ) + "  |  output: " + output ) ;
看这个

mds:[-06-25,--07-22,--08-25,--09-05,--09-28]

mds.get(0)=--06-25 |输出:25/06


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

该项目现已启动,建议迁移到类

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(在你的
      MonthDate
      类中实现
      toString
      方法。你为什么要构建一个自定义类?欢迎使用Stack Overflow。在这个地方,我们不擅长以“我不知道如何开始”的方式提问,抱歉。你必须阅读教程(搜索,你会找到)关于如何创建一个类以及如何控制它的打印方式(搜索
      toString
      )。可能会提前复制感谢!我还是很困惑,你能详细解释一下吗@陈俊杰 java中的每个类都是Object的子类,Object的方法是toString()。这就是System.out.printf为接收到的任何对象调用toString的原因。但是新类的toString的默认实现只是打印该对象的类名和哈希,这不是很有帮助,因此您必须重写默认实现,使其打印您想要的内容,就像Samuel Philipp那样@陈俊杰 我编辑了我的答案,并为
      String.format()
      方法添加了一个解释,希望对您有所帮助。“在print()函数中,您应该使用toString()方法”-实际上,
      System.out.println(t)
      就足够了。toString()将自动调用。这些数字是基础的,编译器说不能应用于int,int。有什么问题吗?mdates[0]=新的SortMonthDate(22,7);mdates[1]=新的SortMonthDate(25,8);mdates[2]=新的SortMonthDate(25,6);mdates[3]=新的SortMonthDate(28,9);mdates[4]=新的SortMonthDate(5,9)@陈俊杰 错误在哪一行?你能按原样放在这里吗?在主要部分,mdates[0]=新的SortMonthDate(22,7);mdates[1]=新的SortMonthDate(25,8);mdates[2]=新的SortMonthDate(25,6);mdates[3]=新的SortMonthDate(28,9);mdates[4]=新的SortMonthDate(5,9);如果你不按原样复制粘贴错误,我就帮不了你。
      MonthDay.of( Month.JUNE , 25 ) )
              .toString()
      
      DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM" ) ;
      String output = mds.get( 0 ).format( f ) ;  // Generate string in custom format.
      
      System.out.println( "mds: " + mds ) ;
      System.out.println( "mds.get( 0 ) = " +  mds.get( 0 ) + "  |  output: " + output ) ;