LoginSignup
2
2

More than 5 years have passed since last update.

文字列型の時刻からyoda-timeのDateTimeをつくる

Posted at

DateTimeのparseメソッドを使う方法

String targetDateTime = "2013-12-10 01:00:00"; //DateTimeに変換する文字列
DateTimeFormatter targetDateTimeFormat = 
      DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); //対象となる時刻のフォーマット
DateTime resultDateTime = DateTiem.parse(targetDate, targetDateTimeFormat);

DateFormatterのparseDateTimeを使う方法

String targetDateTime = "2013-12-10 01:00:00"; 
DateTimeFormatter targetDateTimeFormat = 
      DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); 
DateTime resultDateTime = targetDateTimeFormat.parseDateTime(targetDateTime);

どちらもやっている処理は同じなので、好みによって使い分ければいいと思います。
個人的には、DateTime(targetDate, targetDateTimeFormat)というコンストラクタがあってくれた分かりやすいと思った。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2