将java.time.LocalDate转换为java.util.Date [英] Convert java.time.LocalDate to java.util.Date
本文介绍了将java.time.LocalDate转换为java.util.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有yyyy-MM-dd格式的java.time.LocalDate对象. 我想知道如何将其转换为MM-dd-yyyy格式的java.util.Date. getStartDate()方法应该能够返回格式为MM-dd-yyyy的Date类型的对象.
I have java.time.LocalDate Object in yyyy-MM-dd format. I would like to know how to convert this to java.util.Date with MM-dd-yyyy format. getStartDate() method should be able to return Date type object with the format MM-dd-yyyy.
DateParser类
package com.accenture.javadojo.orgchart;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
public class DateParser {
public static LocalDate parseDate(String strDate){
try{
if((strDate != null) && !("").equals(strDate)){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/yyyy").withLocale(Locale.US);
LocalDate date = LocalDate.parse(strDate, formatter);
return date;
}
} catch (DateTimeParseException e) {
e.printStackTrace();
}
return null;
}
}
public Date getStartDate() {
String fmd = format.format(startDate);
LocalDate localDate = DateParser.parseDate(fmd);
return startDate;
}
推荐答案
如果您有要转换为Date
的LocalDate
,请使用
If you have a LocalDate
which you want to convert to a Date
, use
LocalDate localDate = ...;
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);
来源: http://blog.progs.be/542/date-到Java时间
然后您可以使用SimpleDateFormat
将Date
格式化为所需的任何格式.
You can then use a SimpleDateFormat
to format the Date
to whatever format you like.
这篇关于将java.time.LocalDate转换为java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文