2016-08-23 7 views
-4

私は日が週末(2016年8月28日を言うことができます)ですが、いくつかのarkane理由で、それはいくつかの奇妙なためJavaのカレンダー間違っ日

SimpleDateFormat format = new SimpleDateFormat("yyy,mm,dd"); 
depDate = format.parse(departure_date); 
Calendar calDeparture = Calendar.getInstance(); 
calDeparture.setTime(depDate); 
if (calDeparture.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && calArrival.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { 
     Log.d("true", "WEEKEND"); 
    }else { 
     Log.d("FALSE", "NOT WEEKEND"); 
    } 

を次のようにコードがある間違った日に を返すかどうかを確認したいです私がLog.dを行う理由(「DAY OF WEEK:」、Integer.toString(calDeparture.get(Calendar.DAY_OF_WEEK))); 8月28日に返されます4

+1

「ミリメートル」分、ないヶ月を意味します...あなたは、解析の結果を見れば、あなたは何が起こっているかがわかります。カンマを日付形式で使用することも非常に奇妙です。 –

+0

私は同じ結果を得ます。何も変わりません。 8月28日は週の第4日に印刷されます – denis

+2

その場合、問題を示す[mcve]を提供する必要があります。 –

答えて

1

あなたの書式は間違っています。月は大文字である必要があります。

SimpleDateFormat format = new SimpleDateFormat("yyyy,MM,dd"); 

小文字のmmは分です。

SimpleDateFormat format = new SimpleDateFormat("yyy,MM,dd"); 
    Date depDate = format.parse("2016,8,28"); 
    Calendar calDeparture = Calendar.getInstance(); 
    System.out.println(depDate); 
    calDeparture.setTime(depDate); 
    System.out.println(calDeparture.get(Calendar.DAY_OF_WEEK)); 

出力は次のとおりです。

Sun Aug 28 00:00:00 CEST 2016 
1 
+0

私は同じ結果を得ます。何も変わりません。 8月28日は週の第4日目を印刷します – denis

+0

@denisそれはうまく動作し、1を返します。 – Jens

+0

私はそれを働かせることができました。問題は次のコード行です:departureDateTrue = Integer.toString(year)+ "、" + Integer.toString(month)+ "、" + Integer.toString(day);私はそれを変更しましたdepartureDateTrue = Integer.toString(年)+ "、" + Integer.toString(月+ 1)+ "、" + Integer.toString(日); – denis

関連する問題