通过使用此代码:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(dtStart);
return date;
我已经通过日期对象转换了字符串日期并获得了值:
2013 年 2 月 17 日星期日 07:00:00 GMT
现在我想从这里提取日期(星期日/星期一)和月份。
通过使用此代码:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(dtStart);
return date;
我已经通过日期对象转换了字符串日期并获得了值:
2013 年 2 月 17 日星期日 07:00:00 GMT
现在我想从这里提取日期(星期日/星期一)和月份。
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
你可以试试:
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);
也试试这个:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
自定义星期几,你可以使用这个功能
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 例如 dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
考虑使用 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));
同样在 Kotlin 中:
val string = "2020-01-13T00:00:00"
val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US)
val date = format.parse(string)
val day = DateFormat.format("dd", date) as String
val monthNumber = DateFormat.format("MM", date) as String
val year = DateFormat.format("yyyy", date) as String
在 Kotlin 中,您还可以使用它获取当前日期名称。(需要 API 级别 26)
(Calendar.getInstance() as GregorianCalendar).toZonedDateTime().dayOfWeek
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));
}
例如:如果 stringDate :- 16/12/2018 那么 dateFormat : - dd/MM/yyyy
Calendar calendar = Calendar.getInstance();
DateFormat date= new SimpleDateFormat("EEEE", Locale.getDefault());
String dayName= date.format(calendar.getTime()); //Monday
date = SimpleDateFormat("EE", Locale.getDefault());
dayName = date2.format(calendar.time); //Sun
date= new SimpleDateFormat("dd", Locale.getDefault());
String dayNumber = date.format(calendar.getTime()); //20
date= new SimpleDateFormat("MMM", Locale.getDefault());
String monthName= date.format(calendar.getTime()); //Apr
date= new SimpleDateFormat("MM", Locale.getDefault());
String monthNumber= date.format(calendar.getTime()); //04
date= new SimpleDateFormat("yyyy", Locale.getDefault());
String year= date.format(calendar.getTime()); //2020
而已。请享用
selecteddate = "Tue Nov 26 15:49:25 GMT+05:30 2019";
SimpleDateFormat dateUI = new SimpleDateFormat("EEEE, dd-MMMM-yyyy");
字符串日期 = dateUI.foramt(selecteddate);
Log.e(context,"日期");