![](/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.