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

时间:2019-03-16 11:25:15

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

3 个答案:

答案 0 :(得分:3)

有一个课程!

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 Collections

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格式打印日期,

要了解有关编写课程的信息,请在the source code项目中检出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 ) ;

请参阅此code run live at IdeOne.com

  

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

     

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


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

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

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

答案 1 :(得分:1)

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

要格式化int值,可以使用String.format()方法。格式%02d如果小于10,则用0填充值。要获得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

答案 2 :(得分:0)

假设您的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];
    }
}

我们将daysmonths放入2个不同的数组中,并使用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方法为静态方法之外,还可以使它们不是静态方法,并使用mainmdates.print();中对其进行调用。