2013-01-15 1 views
22

Joda Timeで実際の日時を正しく取得するにはどうすればよいですか?適切には私の国の時間を意味します。私は公式のページとチュートリアルを読んでいます - ロケールとタイムゾーンについては多くのことがありますが、かなり混乱していました。私は単純にそれを得る方法を見つけませんでした。
私は2つのもののためにそれを必要とします:
1)ディスカッションで現在の投稿を取得する
2)私は生年月日と "比較"し、年齢を計算する現在の時間を取得します。
UTC + 1(プラハ - チェコ共和国)の現在の時刻を設定するにはどうすればよいですか?
おかげJoda-Timeで現在の日付と時刻を正しく取得する方法は?

+1

「Javaの日付関連の質問では、「Joda時間を使用する」だけでは答えられません。 – GargantuChet

+0

あなたはjoda time APIを見ましたか? [Joda Time API link](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeZone.html)。これにより、問題を解決するのに役立ちます。 – Smit

+0

これはまた、既に尋ねられた問題をソートするのに役立ちます。 [ジョーダの時間を使用して、指定されたタイムゾーンの現在の壁時間を取得する](http://stackoverflow.com/questions/9184499/using-joda-time-to-get-current-wall-time-for-a-given-タイムゾーン) – Smit

答えて

32

ここはあなたにとって有益なJoda Timeの疑似コードです。

Joda Time API

Link for DateTimeFormat formatter

import org.joda.time.*; 
import org.joda.time.format.DateTimeFormat; 
import org.joda.time.format.DateTimeFormatter; 

public class JodaTimeExample { 

    public static void main(String[] sm) { 
     DateTimeFormatter dateFormat = DateTimeFormat 
       .forPattern("G,C,Y,x,w,e,E,Y,D,M,d,a,K,h,H,k,m,s,S,z,Z"); 

     String dob = "2002-01-15"; 
     LocalTime localTime = new LocalTime(); 
     LocalDate localDate = new LocalDate(); 
     DateTime dateTime = new DateTime(); 
     LocalDateTime localDateTime = new LocalDateTime(); 
     DateTimeZone dateTimeZone = DateTimeZone.getDefault(); 

     System.out 
       .println("dateFormatr : " + dateFormat.print(localDateTime)); 
     System.out.println("LocalTime : " + localTime.toString()); 
     System.out.println("localDate : " + localDate.toString()); 
     System.out.println("dateTime : " + dateTime.toString()); 
     System.out.println("localDateTime : " + localDateTime.toString()); 
     System.out.println("DateTimeZone : " + dateTimeZone.toString()); 
     System.out.println("Year Difference : " 
       + Years.yearsBetween(DateTime.parse(dob), dateTime).getYears()); 
     System.out.println("Month Difference : " 
       + Months.monthsBetween(DateTime.parse(dob), dateTime) 
         .getMonths()); 
    } 
} 

は、私は、これはあなたを助けることを願っています。何か質問があれば教えてください。

P .:出力を与えるためにSumit Aroraに感謝します。

dateFormatr : AD,20,2016,2016,26,2,Tue,2016,180,6,28,PM,8,8,20,20,25,20,2,‌​, 
LocalTime : 20:25:17.308 
localDate : 2016-06-28 
dateTime : 2016-06-28T20:25:18.872+05:30 
localDateTime : 2016-06-28T20:25:20.260 
DateTimeZone : Asia/Kolkata 
Year Difference : 14 
Month Difference : 173 
+2

こんにちは、これはどのように動作するのかを説明する素晴らしい、本当に役立つ例です。大変お世話になり、ありがとうございます:) – user1097772

+1

あなたも出力を見せて欲しいです。 –

+1

@ShajeelAfzal:私が提供した擬似コードは、正しいjarファイルを適用すると自明で実行可能です。とにかく私はこのプログラムを探して(それがまだどこかに座っていれば)、出力を使って答えを更新しようとします。 – Smit

4
LocalTime localTime = new LocalTime(); 
LocalDate localDate = new LocalDate(); 
DateTime dateTime = new DateTime(); 
LocalDateTime localDateTime = new LocalDateTime(); 

このコンストラクタのいずれかがタイムゾーンあなたは誕生日と現在の日付を比較するタイムゾーンDateTimeZone.getDefault();

を意味し、あなたのタイムゾーン、日付を作成します。あなたはどのようにデータベースに日付を保存しますか?
それはUTCタイムゾーンにある場合、あなたはUTC TZの日時と比較することができ

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.UTC)); 

それがサーバーTZを持っている場合は、

Years.yearsBetween(dateOfBirth, new DateTime()); 

を行う必要があり、それはサーバーTZを持っている場合は、

を行う必要があります
Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.forID("ClientTZ"))); 
+0

あなたの応答をありがとう:-) – user1097772

+0

"サーバーTZがあれば"すべきです。サーバーTZとは何ですか? –

+1

@NeonWargeクライアント/サーバアプリケーションのサーバーのタイムゾーンです – Ilya

関連する問題