我想将 LocalDate
转换为 GregorianCalendar
。请指教。
在我的应用程序中,我们使用 GregorianCalendar
来处理日期。但是当我调用其中一个 Web 服务时,我得到了 Joda LocalDate
作为返回类型。
现在我想将 LocalDate
转换为 GregorianCalendar
,因为它与月份值显示不同。请参阅下面的示例代码。
LocalDate
显示 10 月份,而 GregorianCalendar
显示 9 月份 10 月份,因为它从索引 0 开始。请建议如何转换 LocalDate
到 GregorianCalendar
,我尝试查看 API 方法,但没有成功。
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
// Sample code below
LocalDate localDateObj = new LocalDate();
System.out.println("Month value using LocalDate :" + localDateObj.getMonthOfYear());
System.out.println("Date using LocalDate : " + localDateObj);
// Converting LocalDate to DateTime
DateTime dateTimeObj = localDateObj.toDateTimeAtStartOfDay();
System.out.println("DateTime month " + dateTimeObj.getMonthOfYear());
System.out.println("Date using DateTime : " + dateTimeObj);
// GregorianCalendar
GregorianCalendar gc = new GregorianCalendar();
System.out.println("Month value using GregorianCalendar:" + gc.OCTOBER);
System.out.println("GC :" + gc.getTime());
输出:
Month value using LocalDate :10 Date using LocalDate : 2016-10-13 DateTime month 10 Date using DateTime : 2016-10-13T00:00:00.000-04:00 Month value using GregorianCalendar:9 GC :Thu Oct 13 16:14:43 EDT 2016
最佳答案
尝试此示例将 LocalDate
转换为 GregorianCalendar
LocalDate localDateObj = LocalDate.now();
GregorianCalendar gc = GregorianCalendar.from(localDateObj.atStartOfDay(ZoneId.systemDefault()));
注意:gc.OCTOBER
设置为 9,但它表示公历和儒略历中一年中的第十个月。
希望这有帮助!
关于java - 在 Java 中将 LocalDate 转换为 GregorianCalendar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40030300/