簡體   English   中英

iPhone 的 NSCalendar 等效的 Android 是什么?

[英]What is the Android equivalent of iPhone's NSCalendar?

以下 iPhone 代碼的 Android 可能是什么?

NSCalendar *calender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
int units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [calender components:units fromDate:[NSDate date] toDate:destinationDate options:0];

我正在嘗試進行日期倒計時以連續顯示年數、月數、天數、小時數、分鍾數和秒數,而不是整體顯示年數、月數、天數、小時數、分鍾數和秒數。

我有這個,但我無法獲得小時、分鍾和秒。 當我玩時間時,我一直在幾天內得到幾個小時,而不是剩下的幾個小時。

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(myDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        long nat = Math.round(date.getTime() / 1000);
        long totaldifference = Math.abs(d1-d2);

        long date_diff = Math.round(totaldifference/(24*3600));
        //year
        double year2 = Math.floor(date_diff/365);
        date_diff-=year2*365;

        double month2 = Math.floor(date_diff/30.5);
        date_diff-=month2*30.5;

        long day2 = date_diff;

查看日歷的文檔,它應該會有所幫助。 您可以分別獲得年、月、日等,因此應該很容易實現您的倒計時。 它是一個基礎 class 所以你應該能夠擴展它來做你需要的事情。

要獲取Calendar ,您可以執行以下操作:

Calendar cal = Calendar.getInstance()

這樣做會將其初始化為當前日期和時間。 如果您需要將其設置為不同的日期或時間,請查看文檔。

要獲取年、月、日等,您可以這樣做:

int year = cal.get(Calendar.YEAR)
int month = cal.get(Calendar.MONTH)
int day = cal.get(Calendar.DATE)

要“倒計時”,您可以添加要倒計時的任何單位的負數:

cal.add(Calendar.SECOND, -1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM