From a3cd5ce135d910eaa7c841de88cdf65d6581fdd4 Mon Sep 17 00:00:00 2001 From: mkamani Date: Fri, 29 Nov 2024 03:50:26 -0400 Subject: [PATCH] Refactored code by solving design smells present in the code --- src/main/java/org/joda/time/DateTime.java | 11 +++++++++-- src/main/java/org/joda/time/LocalDate.java | 6 +++++- src/main/java/org/joda/time/YearMonth.java | 9 ++++++++- src/main/java/org/joda/time/base/BasePartial.java | 1 + .../java/org/joda/time/TestDateTime_Constructors.java | 3 +++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/joda/time/DateTime.java b/src/main/java/org/joda/time/DateTime.java index cd910e031..964c9da4e 100644 --- a/src/main/java/org/joda/time/DateTime.java +++ b/src/main/java/org/joda/time/DateTime.java @@ -147,7 +147,14 @@ public static DateTime now(Chronology chronology) { */ @FromString public static DateTime parse(String str) { - return parse(str, ISODateTimeFormat.dateTimeParser().withOffsetParsed()); + return ISODateTimeFormat.dateTimeParser().parseDateTime(str); + } + + public static DateTime parse(String str, DateTimeFormatter formatter) { + if (formatter == null) { + throw new IllegalArgumentException("DateTimeFormatter must not be null"); + } + return formatter.parseDateTime(str); } /** @@ -158,7 +165,7 @@ public static DateTime parse(String str) { * @return the parsed date-time, not null * @since 2.0 */ - public static DateTime parse(String str, DateTimeFormatter formatter) { + private static DateTime parseWithFormatter(String str, DateTimeFormatter formatter) { return formatter.parseDateTime(str); } diff --git a/src/main/java/org/joda/time/LocalDate.java b/src/main/java/org/joda/time/LocalDate.java index e4f1a493b..80be2bf69 100644 --- a/src/main/java/org/joda/time/LocalDate.java +++ b/src/main/java/org/joda/time/LocalDate.java @@ -1550,7 +1550,11 @@ public int getDayOfMonth() { * @return the day of week */ public int getDayOfWeek() { - return getChronology().dayOfWeek().get(getLocalMillis()); + return getDayOfWeekField().get(getLocalMillis()); + } + + private DateTimeField getDayOfWeekField() { + return getChronology().dayOfWeek(); } //----------------------------------------------------------------------- diff --git a/src/main/java/org/joda/time/YearMonth.java b/src/main/java/org/joda/time/YearMonth.java index b170f28af..991da48a6 100644 --- a/src/main/java/org/joda/time/YearMonth.java +++ b/src/main/java/org/joda/time/YearMonth.java @@ -870,7 +870,14 @@ public static class Property extends AbstractPartialFieldProperty implements Ser */ @Override public DateTimeField getField() { - return iBase.getField(iFieldIndex); + switch (iFieldIndex) { + case YearMonth.YEAR: + return iBase.getChronology().year(); + case YearMonth.MONTH_OF_YEAR: + return iBase.getChronology().monthOfYear(); + default: + throw new IndexOutOfBoundsException("Invalid index: " + iFieldIndex); + } } /** diff --git a/src/main/java/org/joda/time/base/BasePartial.java b/src/main/java/org/joda/time/base/BasePartial.java index 0dafbf1a4..606fb35fd 100644 --- a/src/main/java/org/joda/time/base/BasePartial.java +++ b/src/main/java/org/joda/time/base/BasePartial.java @@ -19,6 +19,7 @@ import java.util.Locale; import org.joda.time.Chronology; +import org.joda.time.YearMonth; import org.joda.time.DateTimeField; import org.joda.time.DateTimeUtils; import org.joda.time.ReadablePartial; diff --git a/src/test/java/org/joda/time/TestDateTime_Constructors.java b/src/test/java/org/joda/time/TestDateTime_Constructors.java index 1843f290f..457544bd0 100644 --- a/src/test/java/org/joda/time/TestDateTime_Constructors.java +++ b/src/test/java/org/joda/time/TestDateTime_Constructors.java @@ -21,6 +21,7 @@ import junit.framework.TestCase; import junit.framework.TestSuite; +import org.joda.time.DateTime; import org.joda.time.chrono.GregorianChronology; import org.joda.time.chrono.ISOChronology; import org.joda.time.convert.ConverterManager; @@ -171,6 +172,8 @@ public void testParse_noFormatter_vs_constructor_noOffset() throws Throwable { public void testParse_noFormatter_vs_constructor_correctOffset() throws Throwable { DateTime parsed = DateTime.parse("2010-06-30T01:20+01:00"); DateTime constructed = new DateTime("2010-06-30T01:20+01:00"); + DateTime parsedNormalized = parsed.withZoneRetainFields(DateTimeZone.UTC); + DateTime constructedNormalized = constructed.withZoneRetainFields(DateTimeZone.UTC); assertEquals(DateTimeZone.getDefault(), constructed.getZone()); assertEquals(DateTimeZone.forOffsetHours(1), parsed.getZone()); }