2012-03-30 5 views
0
Javaで日付のこのタイプから別の変数に日付と時刻を格納するための方法

、IN 2時24分PM日のこのタイプからの時間と日付を取得するために、どのように2011年8月29日には、JavaやAndroid

Aug 29 2011 2:24PM 

私は店を望みますdate = 8/29/2011、time = 2:24 PM

これは可能ですか?

+2

分割が意味してください??? –

+0

何を得るために分割? –

+1

あなたは 'String.split(" ");'を探していないと思っていますが、何をしたいですか?あなたは入力または日付の文字列を持っていますか? –

答えて

4

この

String inputDateInString = "Aug 29 2011 2:24PM";     
DateFormat formatter = new SimpleDateFormat("MMM dd yyyy h:mmaa"); 
try { 
    Date dateObject = formatter.parse(inputDateInString); 

    String date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject); 
    String time = new SimpleDateFormat("h:mmaa").format(dateObject); 
} catch (ParseException e) { 
e.printStackTrace(); 
} 
0

はここ

Date today = new Date(); 
DateFormat format = new SimpleDateFormat("MM/dd/yyyy"); 
DateFormat year = new SimpleDateFormat("yyyy"); 
DateFormat month = new SimpleDateFormat("MM"); 
DateFormat day = new SimpleDateFormat("dd"); 
System.out.println("today is: " + format.format(today)); 
System.out.println("The year is: " + year.format(today)); 
System.out.println("The month is: " + month.format(today)); 
System.out.println("The day is: " + day.format(today)); 
0

は実際に、あなたの質問は非常に明確ではありませんのSimpleDateFormatクラスを使用した例ですが、私は、あなたが一日、月、文字列の日付からなどのフィールドを取得したいと仮定します。 SimpleDateFormatを使用してこれを実現します。

String date="Aug 29 2011 2:24PM"; 
DateFormat format = new SimpleDateFormat("MMM dd yyyy HH:mm"); 
Date dt= format.parse(date); 
Calendar calendar=Calendar.getInstance(); 
calendar.setDate(dt); 
int d= calendar.get(Calendar.DAY_OF_MONTH); 
0

年、月、週、日などを保持したい場合は、カレンダークラスを探してください。

Calendar c = Calendar.getInstance(); 
System.out.println("Year: " + c.get(Calendar.YEAR)); 
System.out.println("Month: " + c.get(Calendar.MONTH)); 
.... 
関連する問題