开发者

Jodatime - Custom Chronology

I would like to calculate a duration whereas 1 day will consist of 8 hours. Is creating a new Chronology is the right approach ?

I did try to create a custom Chronology and override the assemble method with something like:

int millisPerDay = 1000 * 60 * 60 * 8;

fields.days =开发者_如何学运维 new PreciseDurationField(DurationFieldType.days(), millisPerDay);

However it did not have the desired effect.

Any suggestions?

Thank you in advance


To answer my own question, following is a complete working Chronology class (for 8 hours days). I am posting it here so it might help other people that trying to solve similar problems.


import org.joda.time.Chronology;
import org.joda.time.DateTimeZone;
import org.joda.time.DurationFieldType;
import org.joda.time.field.PreciseDurationField;
import org.joda.time.chrono.AssembledChronology;
import org.joda.time.chrono.GregorianChronology;

public final class EightHoursDayChronology extends AssembledChronology {

   private static final EightHoursDayChronology INSTANCE_UTC;

  static{
  INSTANCE_UTC = new EightHoursDayChronology(GregorianChronology.getInstanceUTC());
  }


  public static EightHoursDayChronology getInstance(){
       return INSTANCE_UTC  ;
  }

    private EightHoursDayChronology(Chronology base) {
        super(base, null);
    }

    @Override
   protected void assemble(org.joda.time.chrono.AssembledChronology.Fields fields) {

    int millisPerDay = 1000 * 60 * 60 * 8   ;
    fields.days = new PreciseDurationField(DurationFieldType.days(), millisPerDay)
  }

   @Override
  public Chronology withUTC() {
    return INSTANCE_UTC    ;
  }

    @Override
    public Chronology withZone(DateTimeZone zone) {
        throw new UnsupportedOperationException("Method was not implemented");
    }

    @Override
    public String toString() {
        return "EightHoursDayChronology";
    }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜