我有一個 int 格式的日期 DDMMYYYY,我怎樣才能分隔日月年 (I have a date in int format DDMMYYYY, how can I separate day and month and year)


問題描述

我有一個 int 格式的日期 DDMMYYYY,我怎樣才能分隔日月年 (I have a date in int format DDMMYYYY, how can I separate day and month and year)

int date1 = 22092021
int date2 = 03122021

假設每個月有 30 天,我想知道哪個日期更早


參考解法

方法 1:

Apart from the obvious problems with this representation (see comments) the parts could be separated like this:

int y = date % 10000
date /= 10000
int m = date % 100
int d = date / 100

方法 2:

As pointed out in comments, it's most likely that input might be coming as string. You can easily parse Date from string like this:

private static Date getDate(String dateStr) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMyyyy");
    return simpleDateFormat.parse(dateStr);
}

Then you can do something like this to check which date is older:

String date1 = "22092021";
String date2 = "03122021";

Date d1 = getDate(date1);
Date d2 = getDate(date2);

if (d1.compareTo(d2) < 0) {
    System.out.println("d1 is older than d2");
} else if (d1.compareTo(d2) > 0) {
    System.out.println("d2 is older than d1");
} else {
    System.out.println("both are equal");
}

In case you're interested in extracting day, month and year from your Date instance, you can create a small utility method to convert it to Calendar like this:

private static Calendar toCalendar(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal;
}

Then you can extract it with Calendar::get methods like this:

Calendar c1 = toCalendar(d1);
System.out.printf("%d %d %d\n", c1.get(Calendar.DAY_OF_MONTH), c1.get(Calendar.MONTH), c1.get(Calendar.YEAR));

Calendar c2 = toCalendar(d2);
System.out.printf("%d %d %d\n", c2.get(Calendar.DAY_OF_MONTH), c2.get(Calendar.MONTH), c2.get(Calendar.YEAR));

(by Edgero30HenryRohan Kumar)

參考文件

  1. I have a date in int format DDMMYYYY, how can I separate day and month and year (CC BY‑SA 2.5/3.0/4.0)

#java #datetime-format #Date






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論