我正在使用以下代码来获取即将到来的星期日的日期。当我的设备语言是英语时,它正常工作。但是,当设备语言改为西班牙语时,不是这样。
例如:英语为2015-11-22,但西班牙语为2015-11-29。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
// get start of this week in milliseconds
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
calendar.add(Calendar.DAY_OF_WEEK, 7);
Date currentTime = calendar.getTime();
date = dateFormat.format(currentTime);
答案 0 :(得分:0)
将SimpleDateFormat的声明更改为:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.US);
它适用于所有语言。
答案 1 :(得分:0)
实际上,你得到了错误的一天:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
// get start of this week in milliseconds
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, calendar.SUNDAY);
Date currentTime = calendar.getTime();
String date = dateFormat.format(currentTime);