2013-07-14 2 views
-5

** 정의 된 두 시간 (예 : 12.45 ~ 21:00) 사이의 시간을 계산하는 안드로이드 응용 프로그램을 만들고 싶습니다. 어떻게 할 수 있습니까? **정의 된 두 시간 사이의 카운트 다운?

//////// /////////////////////////////////////////////////////////////////// ///////////////

이 방법으로 시도했습니다. 나는 고장

f` Integer year = null; 
    Integer monthOfYear = null; 
    Integer dayOfMonth = null; 
    Integer hourOfDay = null; 
    Integer minuteOfHour = null; 
    Integer secondOfMinute = null; 




     DateTime(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute); 


     DateTime start = new DateTime(2013, 07, 14, 12, 45, 0); 
     DateTime end = new DateTime(2013, 07, 14, 21, 0, 0); 

     Seconds diff = Seconds.secondsBetween(start, end); 
     mTextField.setText(diff.getSeconds());` 

내가 어떻게 할 수 있다고 생각?

+1

당신의 질문은 안드로이드 앱을 개발하는 방법이나 diff를 찾는 방법과 관련이 있습니다. 시간 사이?. 질문을 편집하여 세부 사항을 추가하십시오. –

+0

첫 번째 줄이 무엇인지 모르겠지만 마지막 네 줄은 잘 작동합니다. 지금 당장 시도했습니다. –

답변

2

joda.time library을 사용하면 시간을 빼고 결과를 몇 초 만에 얻을 수 있습니다.

DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, DateTimeZone zone)과 같이 int를 사용하여 시간을 전송할 수있는 생성자를 사용하여 DateTime objects을 두 개 만듭니다.

DateTime start = new DateTime(2013, 07, 14, 12, 45, 0, zone); 
DateTime end = new DateTime(2013, 07, 14, 21, 0, 0, zone); 

yoda-time seconds to find the diference에 대한 함수를 사용할 수 있습니다.

Seconds diff = Seconds.secondsBetween(start, end); 

편집 :는 주요 방법으로 표준 자바 클래스 여기에 사용되는 코드 예제를 표시하려면 다음

import org.joda.time.DateTime; 
import org.joda.time.Seconds; 


public class Test { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     DateTime start = new DateTime(2013, 07, 14, 12, 45, 0); 
      DateTime end = new DateTime(2013, 07, 14, 21, 0, 0); 

      Seconds diff = Seconds.secondsBetween(start, end); 
      System.out.println(diff.getSeconds()); 

    } 

} 

다음 크리스마스

때까지 초를 계산하는 또 다른 예
public static void main(String[] args) { 
    DateTime start = new DateTime(); 
    DateTime end; 
    if(start.getDayOfMonth()>24 && start.getMonthOfYear()==12) 
    { 
     end = new DateTime(start.getYear()+1, 12, 25, 0, 0, 0); 
    } 
    else 
    { 
     end = new DateTime(start.getYear(), 12, 25, 0, 0, 0); 
    } 
     Seconds diff = Seconds.secondsBetween(start, end); 
     System.out.println(diff.getSeconds()); 

} 

언급했듯이 joda 시간없이이 모든 작업을 수행 할 수 있습니다. 이 경우에는 1970 년 이래로 밀리 초 단위의 타임 스탬프를 제공하는 getTime()이라는 메소드가있는 java.util.Date를 사용하게됩니다. 두 개의 java.util.Date 객체 (long 유형 임)에서 타임 스탬프를 얻으면) 그리고 두 날짜 사이의 밀리 초를 빼십시오. 이 값을 1000 초로 줄이면 초가됩니다. joda time 접근 방식과 마찬가지로 객체 지향적이지는 않지만 쉽습니다.

은 위의 경우이 될 것이다 :

import java.util.Date; 




public class Test { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    Date start = new Date(2013, 07, 14, 12, 45, 0); 
    Date end = new Date(2013, 07, 14, 21, 0, 0); 
    System.out.println((end.getTime()-start.getTime())/1000); 
} 

}

그러나이 java.util.Date의 생성자는 사용되지 않으며 더 이상 사용해서는 안된다. 날짜를 처리하는 새로운 방법은 일종의 희귀 한 and is documented here입니다.

+0

외부 라이브러리가 필요하지 않습니다.이 라이브러리 없이도 수행 할 수 있습니다. – BackSlash

+1

yodatime 일 수는 있지만 훨씬 더 좋습니다. –

+0

İ've이 시도했지만 작동하지 않았다 – Cracked

관련 문제