samedi 19 août 2017

Parametrize ZonedDateTime method

I have a parametrized method:

private static Date getTimeFor(int hour, int minute, int second, int nanoSecond, String zoneId) {
    ZonedDateTime time = ZonedDateTime.now()
            .withZoneSameLocal(ZoneId.of(zoneId))
            .withHour(hour)
            .withMinute(minute)
            .withSecond(second)
            .withNano(nanoSecond);
    return Date.from(time.toInstant());
}

Followed by:

    private Date getRomeStartTime(){
        return getTimeFor(8,30,0,0,"Europe/Rome");
    }

    private Date getParisStartTime(){
        return getTimeFor(9,30,0,0,"Europe/Paris");
    }

    private Date getLondonStartTime(){
        return getTimeFor(9,00,0,0,"Europe/London");
    }

This can get out of hand quickly as more cities are added. I'm aiming to only make public/expose the below method and delegate the construction of StartTimes elsewhere:

public Date getEffectiveTimeFor(String zoneId){
        // Delegate construction as per zoneId elsewhere
        // dont want to use a long-winded if-else statement
    }

I cant use the strategy pattern as I should not pass an object, but only a string. What is the best approach here?

(p.s. I have to return the old Date, this is beyond the question )

Aucun commentaire:

Enregistrer un commentaire