package com.java.myapp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyClass { public static void main(String[] args) { String startDate = "2013-08-01"; String endDate = "2013-08-12"; int dayDiff = dateDiff(startDate,endDate); System.out.println("Date Diff " + startDate + " and " + endDate + " = " + dayDiff); } public static int dateDiff(String startDate,String endDate) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date startdate = df.parse(startDate); Date enddate = df.parse(endDate); long diff = enddate.getTime() - startdate.getTime(); int dayDiff = (int) (diff / (24 * 60 * 60 * 1000)); return dayDiff; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } }
package com.java.myapp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyClass { public static void main(String[] args) { String startTime = "2013-08-01 12:43:23"; String endTime = "2013-08-02 21:01:21"; DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date start = df.parse(startTime); Date end = df.parse(endTime); long diff = end.getTime() - start.getTime(); int dayDiff = (int) (diff / (24 * 60 * 60 * 1000)); int hourDiff = (int) (diff / (60 * 60 * 1000) % 24); int minuteDiff = (int) (diff / (60 * 1000) % 60); int secondDiff = (int) (diff / 1000 % 60); System.out.println("Day Diff = " + dayDiff); System.out.println("Hour Diff = " + hourDiff); System.out.println("Minute Diff = " + minuteDiff); System.out.println("Second Diff = " + secondDiff); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท