En utilisant ce code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(dtStart);
return date;
J'ai converti l'objet Date par date de chaîne et récupéré la valeur ... -
Maintenant, je veux extraire le jour (dimanche/lundi) et le formulaire mois ici toute aide ????
import Android.text.format.DateFormat;
String dayOfTheWeek = (String) DateFormat.format("EEEE", date); // Thursday
String day = (String) DateFormat.format("dd", date); // 20
String monthString = (String) DateFormat.format("MMM", date); // Jun
String monthNumber = (String) DateFormat.format("MM", date); // 06
String year = (String) DateFormat.format("yyyy", date); // 2013
Tu peux essayer:
String input_date="01/08/2012";
SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
Date dt1=format1.parse(input_date);
DateFormat format2=new SimpleDateFormat("EEEE");
String finalDay=format2.format(dt1);
Essayez aussi ceci:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
aux jours de la semaine personnalisés, vous pouvez utiliser cette fonction
public static String getDayFromDateString(String stringDate,String dateTimeFormat)
{
String[] daysArray = new String[] {"saturday","sunday","monday","tuesday","wednesday","thursday","friday"};
String day = "";
int dayOfWeek =0;
//dateTimeFormat = yyyy-MM-dd HH:mm:ss
SimpleDateFormat formatter = new SimpleDateFormat(dateTimeFormat);
Date date;
try {
date = formatter.parse(stringDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-1;
if (dayOfWeek < 0) {
dayOfWeek += 7;
}
day = daysArray[dayOfWeek];
} catch (Exception e) {
e.printStackTrace();
}
return day;
}
dateTimeFormat pour exemple dateTimeFormat = "aaaa-MM-jj HH: mm: ss";
Pensez à utiliser la classe Java.util.Calendar.
String dateString = "20/12/2018";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date readDate = df.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(readDate.getTime());
Log.d(TAG, "Year: "+cal.get(Calendar.YEAR));
Log.d(TAG, "Month: "+cal.get(Calendar.MONTH));
Log.d(TAG, "Day: "+cal.get(Calendar.DAY_OF_MONTH));
public static String getDayFromStringDate(String stringDate, String dateFormat, boolean abbreviated) throws ParseException {
String pattern;
if (abbreviated) {
pattern = "E"; // For short day eg: Mon,Tue
} else {
pattern = "EEEE"; // For compete day eg: Monday, Tuesday
}
return new SimpleDateFormat(pattern)
.format(new SimpleDateFormat(dateFormat).parse(stringDate));
}
Ex: si chaîne Date: - 16/12/2018 la date Format: - JJ/MM/AAAA