下面列出了org.joda.time.LocalDate#toDate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 根据周数,获取开始日期、结束日期
*
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
* @return 返回date[0]开始日期、date[1]结束日期
*/
public static Date[] getWeekStartAndEnd(int week) {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
date = date.dayOfWeek().withMinimumValue();
Date beginDate = date.toDate();
Date endDate = date.plusDays(6).toDate();
return new Date[]{beginDate, endDate};
}
public static Date toDate(String dateString) {
if (StringUtils.isEmpty(dateString)) {
throw new IllegalArgumentException("date string cannot be empty");
}
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
LocalDate dateTime = dtf.parseLocalDate(dateString);
return dateTime.toDate();
}
/**
* 根据周数,获取开始日期、结束日期
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
* @return 返回date[0]开始日期、date[1]结束日期
*/
public static Date[] getWeekStartAndEnd(int week) {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
date = date.dayOfWeek().withMinimumValue();
Date beginDate = date.toDate();
Date endDate = date.plusDays(6).toDate();
return new Date[]{beginDate, endDate};
}
/**
* 根据周数,获取开始日期、结束日期
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周
* @return 返回date[0]开始日期、date[1]结束日期
*/
public static Date[] getWeekStartAndEnd(int week) {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.plusWeeks(week));
date = date.dayOfWeek().withMinimumValue();
Date beginDate = date.toDate();
Date endDate = date.plusDays(6).toDate();
return new Date[]{beginDate, endDate};
}
private boolean isADate(JSONObject jsonConf, IDataStore dataStore, int columnIndex) throws JSONException {
String dateFormat = jsonConf.get(DataSetConstants.FILE_DATE_FORMAT).toString();
for (int i = 0; i < Math.min(10, dataStore.getRecordsCount()); i++) {
IRecord record = dataStore.getRecordAt(i);
IField field = record.getFieldAt(columnIndex);
Object value = field.getValue();
if (value instanceof Date) {
if (value instanceof Timestamp)
return false;
// it's already a Date, skip the check
continue;
}
try {
// JDK 8 version
/*
* DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat); LocalDate localDate = LocalDate.parse((String) field.getValue(),
* formatter); Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
*/
DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat);
LocalDate localDate = LocalDate.parse((String) field.getValue(), formatter);
localDate.toDate();
} catch (Exception ex) {
logger.debug(field.getValue() + " is not a date");
return false;
}
}
return true;
}
public static Date toDate(Object dateString) {
if (dateString == null) {
throw new IllegalArgumentException("date string cannot be empty");
}
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
LocalDate dateTime = dtf.parseLocalDate((String) dateString);
return dateTime.toDate();
}
@Override
public IAnswerData getAnswer() {
mDatePicker.clearFocus();
LocalDate ldt = new LocalDate(mDatePicker.getYear(), mDatePicker.getMonth() + 1,
mDatePicker.getDayOfMonth());
return new DateData(ldt.toDate());
}
@Override
public Date convertToDatabaseColumn(LocalDate attribute) {
return attribute == null ? null : attribute.toDate();
}
@Override
public Date convert(LocalDate in, Context context) throws Exception {
if (in == null) return null;
return in.toDate();
}
public static Date addDate(Date startDate, Integer years, Integer months, Integer days) {
LocalDate currentDate = new LocalDate(startDate);
currentDate = currentDate.plusYears(years);
currentDate = currentDate.plusMonths(months);
currentDate = currentDate.plusDays(days);
return currentDate.toDate();
}
public static Date subtractDate(Date startDate, Integer years, Integer months, Integer days) {
LocalDate currentDate = new LocalDate(startDate);
currentDate = currentDate.minusYears(years);
currentDate = currentDate.minusMonths(months);
currentDate = currentDate.minusDays(days);
return currentDate.toDate();
}
public static Date addDate(Object startDate, Object years, Object months, Object days) {
LocalDate currentDate = new LocalDate(startDate);
currentDate = currentDate.plusYears(intValue(years));
currentDate = currentDate.plusMonths(intValue(months));
currentDate = currentDate.plusDays(intValue(days));
return currentDate.toDate();
}
public static Date subtractDate(Object startDate, Object years, Object months, Object days) {
LocalDate currentDate = new LocalDate(startDate);
currentDate = currentDate.minusYears(intValue(years));
currentDate = currentDate.minusMonths(intValue(months));
currentDate = currentDate.minusDays(intValue(days));
return currentDate.toDate();
}