From 0df320956b585396dde9055fb87f9593257d3e21 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 7 Mar 2017 13:13:55 +0100 Subject: [PATCH] fixed files form Time #12 --- projects/Time/12/org/joda/time/LocalDate.java | 8 +++++++- projects/Time/12/org/joda/time/LocalDateTime.java | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/projects/Time/12/org/joda/time/LocalDate.java b/projects/Time/12/org/joda/time/LocalDate.java index 3868531..c86b9d7 100644 --- a/projects/Time/12/org/joda/time/LocalDate.java +++ b/projects/Time/12/org/joda/time/LocalDate.java @@ -207,9 +207,10 @@ public static LocalDate fromCalendarFields(Calendar calendar) { if (calendar == null) { throw new IllegalArgumentException("The calendar must not be null"); } + int era = calendar.get(Calendar.ERA); int yearOfEra = calendar.get(Calendar.YEAR); return new LocalDate( - yearOfEra, + (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH) ); @@ -240,7 +241,12 @@ public static LocalDate fromDateFields(Date date) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } + if (date.getTime() < 0) { // handle years in era BC + GregorianCalendar cal = new GregorianCalendar(); + cal.setTime(date); + return fromCalendarFields(cal); + } return new LocalDate( date.getYear() + 1900, date.getMonth() + 1, diff --git a/projects/Time/12/org/joda/time/LocalDateTime.java b/projects/Time/12/org/joda/time/LocalDateTime.java index f37db3e..e75deca 100644 --- a/projects/Time/12/org/joda/time/LocalDateTime.java +++ b/projects/Time/12/org/joda/time/LocalDateTime.java @@ -196,9 +196,10 @@ public static LocalDateTime fromCalendarFields(Calendar calendar) { if (calendar == null) { throw new IllegalArgumentException("The calendar must not be null"); } + int era = calendar.get(Calendar.ERA); int yearOfEra = calendar.get(Calendar.YEAR); return new LocalDateTime( - yearOfEra, + (era == GregorianCalendar.AD ? yearOfEra : 1 - yearOfEra), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), @@ -233,7 +234,12 @@ public static LocalDateTime fromDateFields(Date date) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } + if (date.getTime() < 0) { // handle years in era BC + GregorianCalendar cal = new GregorianCalendar(); + cal.setTime(date); + return fromCalendarFields(cal); + } return new LocalDateTime( date.getYear() + 1900, date.getMonth() + 1,