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...

3,199 Responses

  1. carmvaly表示:

    This application is supported on all major operating systems, including Windows, Mac, GNU/Linux, Solaris, Windows CE and more.
    The user interface is completely covered by a scrollable window, which makes the viewing interface extremely easy to use, even for beginners.
    Following parameters can be set to crops the image with ease:

    Crop Region:

    Image Thumbnail Dimension:

    Crop Left and Right Margins:

    Crop Top https://barcuddhydtu.weebly.com

    6add127376 carmvaly

  2. elisema表示:

    That is what we love about software developer Igor who made this developer tool available to the public.Photo: Doina Chivu/Cultura RM/ via Getty Images

    The music industry has changed, but a gazillion new female guitarists change it even more. Writing for Paste, Mick LaSalle highlights six artists making electro-pop music in the digital age:

    I’m aware of plenty of artists who’d drop to their collective knees http://twcmail.de/deref.php?https://lastiotaka.weebly.com

    6add127376 elisema

  3. gitunad表示:

    There are no unnecessary applications, as well as the app doesn’t have unnecessary features, which makes it easy to use, without taking up much of your computer’s memory. Moreover, this easy to use app is free, without the need to upgrade, so it’s available for all users.Q:

    Is there a difference between these two ways of declaring a class object?

    Today I was looking at some old code that I’d written. I’d written in C++ some years https://liousamiril.weebly.com

    6add127376 gitunad

  4. DouglasLep表示:

    clomid tablets buy clomid clomid tablets

  5. DouglasLep表示:

    buy clomid 50mg online buy clomid 50mg clomid tablets

  6. Stromsob表示:

    pharmacy without dr prescriptions meds online without doctor prescription aarp approved canadian online pharmacies

發佈留言

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