close

 

標題:

JAVA 急~~~兩個日期相差幾天

發問:

想了很久也試了很久 但還是做不出來:(( 幫幫我 先輸入第一次日期yyyy/mmm/dd 再輸入第二次日期yyyy/mmm/dd 這兩個日期相差幾天 程式碼要簡單一點 拜託了>__
最佳解英文佳句答:

import java.util.*; import java.text.*; public class Program { public static void main(String[] args) { Date today = getAsDate("2013/jan/1"); Date tomorrow = getAsDate("2013/feb/2"); int daysInBetween = getDaysInBetween(today, tomorrow); System.out.printf("%s => %s = %s days", today, tomorrow, daysInBetween); System.exit(0); } public static Date getAsDate(String value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MMM/dd"); Date result = null; try { result = sdf.parse(value); } catch (ParseException ignore) { } return result; } public static int getDaysInBetween(Date one, Date two) { Calendar start = getAsCalendar(one); Calendar end = getAsCalendar(two); int result = 0; while(start.before(end)) { start.add(Calendar.DAY_OF_MONTH, 1); result++; } return result; } private static Calendar getAsCalendar(Date value) { Calendar result = GregorianCalendar.getInstance(); result.setTime(value); return result; } }

其他解答:

英語教學英文諺語

6BD45AA720B3A9F1

arrow
arrow
    創作者介紹

    林俊德脹尼莊芒幫 發表在 痞客邦 留言(0) 人氣()