Java 各種時間上的操作範例

分享一些在專案中用到JAVA與日期相關的操作,包括:

  • 取得目前的年、月、日
  • 判斷兩個日期的大小
  • 計算兩個日期的差距
  • 取得昨天的日期
  • 取得上個月的開始與結束日
package CDIT.stanley;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;

public class dateOperation {
	
	public static int differentDays(Date date1,Date date2){
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2){
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++){
                if(i%4==0 && i%100!=0 || i%400==0){
                    timeDistance += 366;
                }
                else{
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2-day1) ;
        }
        else{
            return day2-day1;
        }
    }
	
	public static Date getFirstMonthDay(Calendar calendar) {
		calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
		return calendar.getTime();
	}

	public static Date getLastMonthDay(Calendar calendar) {
		calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
		return calendar.getTime();
	}
	
    public static void main(String[] args) throws ParseException {
    	
    	//取得目前的年、月、日
		Calendar calendar = Calendar.getInstance();	
		System.out.println("今天是" + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");
		System.out.println("==================================================");
    	//輸出:今天是2017年8月24日
		
		//判斷兩個日期的大小
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.TAIWAN);
    	Date date1 = sdf.parse("2017-08-23");
    	Date date2 = sdf.parse("2016-09-22");
		System.out.println("Date1 < Date2 : " + date1.before(date2));
		System.out.println("Date1 > Date2 : " + date1.after(date2));
		System.out.println("==================================================");
		//輸出:Date1 < Date2 : false、Date1 > Date2 : true
		
		//計算兩個日期的差距
		System.out.println("Date1 & Date2 差距 : " + differentDays(date1 , date2) + "天");
		System.out.println("==================================================");
		//輸出:Date1 & Date2 差距 : 31天
		
		//取得昨天的日期
		calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String  yestedayDate = sdf.format(calendar.getTime());
		System.out.println("昨天是" + yestedayDate);
		System.out.println("==================================================");
		//輸出:昨天是2017-08-23
		
		//取得上個月的開始與結束日
		calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH,-1);
		String monthDayFirst = sdf.format(getFirstMonthDay(calendar));
		String monthDayLast = sdf.format(getLastMonthDay(calendar));
		System.out.println("上個月的第一天是" + monthDayFirst);
		System.out.println("上個月的最後一天是" + monthDayLast);
		System.out.println("==================================================");
		//輸出:上個月的第一天是2017-07-01、上個月的最後一天是2017-07-31
    }
}

You may also like...

10,907 Responses

  1. As maiores probabilidades neste cassino Tigrinho

  2. Robertdiz表示:

    pharmacie en ligne pas cher: cialis generique – п»їpharmacie en ligne france

  3. Allenacurf表示:

    pharmacie en ligne sans ordonnance: achat kamagra – pharmacie en ligne france livraison belgique

  4. Cassinos com condicoes favoraveis Tigrinho

  5. IgnacioSlima表示:

    Pharmacie en ligne livraison Europe: Acheter Cialis – pharmacie en ligne fiable

  6. JuliusIntal表示:

    pharmacie en ligne france pas cher: kamagra oral jelly – pharmacie en ligne france pas cher

  7. view products表示:

    Today, taking into consideration the fast life-style that everyone is having, credit cards have a big demand in the economy. Persons from every discipline are using credit card and people who not using the card have prepared to apply for just one. Thanks for discussing your ideas on credit cards.

  8. click here表示:

    We are a group of volunteers and opening a new scheme in our community. Your site offered us with valuable information to work on. You have done a formidable job and our whole community will be thankful to you.

  9. cock sleeve表示:

    I’ve really noticed that credit improvement activity needs to be conducted with tactics. If not, chances are you’ll find yourself destroying your rank. In order to succeed in fixing your credit rating you have to ascertain that from this moment in time you pay your entire monthly costs promptly prior to their timetabled date. It is definitely significant for the reason that by not really accomplishing that, all other steps that you will take to improve your credit position will not be efficient. Thanks for sharing your concepts.

  10. tips to use表示:

    I love what you guys are usually up too. This type of clever work and reporting! Keep up the awesome works guys I’ve you guys to blogroll.

  11. Allenacurf表示:

    Viagra Pfizer sans ordonnance: viagra sans ordonnance – Viagra gГ©nГ©rique pas cher livraison rapide

  12. Robertdiz表示:

    pharmacie en ligne france pas cher: Levitra pharmacie en ligne – Pharmacie en ligne livraison Europe

  13. Allenacurf表示:

    Achat mГ©dicament en ligne fiable: Levitra pharmacie en ligne – vente de mГ©dicament en ligne

  14. IgnacioSlima表示:

    Viagra 100mg prix: Acheter du Viagra sans ordonnance – Viagra homme sans ordonnance belgique

  15. Allenacurf表示:

    pharmacie en ligne france livraison internationale: cialis sans ordonnance – pharmacie en ligne france pas cher

  16. IgnacioSlima表示:

    pharmacie en ligne france fiable: pharmacie en ligne sans ordonnance – trouver un mГ©dicament en pharmacie

  17. Allenacurf表示:

    Pharmacie Internationale en ligne: Levitra sans ordonnance 24h – pharmacie en ligne pas cher

  18. Allenacurf表示:

    pharmacie en ligne france pas cher: levitra generique prix en pharmacie – pharmacie en ligne avec ordonnance

  19. Allenacurf表示:

    pharmacie en ligne france livraison internationale: pharmacie en ligne pas cher – pharmacie en ligne

  20. Allenacurf表示:

    pharmacie en ligne france livraison belgique: pharmacie en ligne france livraison belgique – pharmacie en ligne france pas cher

  21. GeorgeDaf表示:

    Pharmacie en ligne livraison Europe: pharmacie en ligne pas cher – Achat mГ©dicament en ligne fiable

  22. IgnacioSlima表示:

    pharmacie en ligne avec ordonnance: levitra generique sites surs – Pharmacie sans ordonnance

  23. GeorgeDaf表示:

    SildГ©nafil 100 mg sans ordonnance: viagra sans ordonnance – SildГ©nafil 100mg pharmacie en ligne

  24. Allenacurf表示:

    pharmacie en ligne sans ordonnance: pharmacie en ligne france pas cher – pharmacie en ligne france fiable

  25. GeorgeDaf表示:

    Viagra pas cher inde: Viagra generique en pharmacie – Viagra pas cher paris

  26. RobertBlica表示:

    http://levitraenligne.com/# pharmacie en ligne france fiable

  27. Eu recomendo um cassino confiavel Tigrinho

  28. Os melhores bonus deste cassino Tigrinho

  29. Allenacurf表示:

    Pharmacie sans ordonnance: achat kamagra – pharmacie en ligne livraison europe

  30. Jogue em um cassino confiavel Tigrinho

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。