Java 8 Duration - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Duration class.

This code example shows how to use the following methods:getDays, getHours, getMinutes, getMonths, getSeconds
  private Calendar add(Calendar date, Duration duration) {
    Calendar calendar = (Calendar) date.clone();
 
    // duration.addTo does not account for daylight saving time (xerces), 
    // reversing order of addition fixes the problem 
    calendar.add(Calendar.SECOND, duration.getSeconds() * duration.getSign());
    calendar.add(Calendar.MINUTE, duration.getMinutes() * duration.getSign());
    calendar.add(Calendar.HOUR, duration.getHours() * duration.getSign());
    calendar.add(Calendar.DAY_OF_MONTH, duration.getDays() * duration.getSign());
    calendar.add(Calendar.MONTH, duration.getMonths() * duration.getSign());
    calendar.add(Calendar.YEAR, duration.getYears() * duration.getSign());
 
    return calendar;
  } 
 
  private Calendar parseDate(String date) throws Exception {
    return ISODateTimeFormat.dateTimeParser().withZone(DateTimeZone.forTimeZone(clockReader.getCurrentTimeZone())).parseDateTime(date).toCalendar(null);
  } 
 
  private Duration parsePeriod(String period) throws Exception {
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getDays, getMonths, getYears
		add(secsField);	
			 
	} 
	 
	public void setDuration(Duration duration){
		yearField.setText(Integer.toString(duration.getYears()));
		monthsField.setText(Integer.toString(duration.getMonths()));
		daysField.setText(Integer.toString(duration.getDays()));
		hoursField.setText(Integer.toString(duration.getDays()));
		minsField.setText(Integer.toString(duration.getDays()));
		secsField.setText(Integer.toString(duration.getDays()));
	} 
	 
	public Duration getDuration(){
		try { 
			DatatypeFactory dbf = DatatypeFactory.newInstance();
			Duration duration = dbf.newDuration(true, 
					Integer.parseInt(yearField.getText()), 
					Integer.parseInt(monthsField.getText()), 
					Integer.parseInt(daysField.getText()), 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
  } 
 
  @Override 
  public int compare(Duration duration)
  { 
    return this.duration.compare(duration);
  } 
 
  @Override 
  public Number getField(Field field)
  { 
    return duration.getField(field);
  } 
 
  @Override 
  public int getSign() 
  { 
    return duration.getSign();
  } 
 
3
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getDays, getHours, getMinutes, getSeconds
 
    @Test
    public void parseShouldWork() throws Exception {
        Duration duration = TimeSpan8601Converter.parse("P0DT3H10M2S");
 
        assertEquals(0, duration.getDays());
        assertEquals(3, duration.getHours());
        assertEquals(10, duration.getMinutes());
        assertEquals(2, duration.getSeconds());
    } 
} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getDays, getHours, getMinutes, getMonths, getSeconds
        if (loc == null) {
            cal = Calendar.getInstance(getXMLConversionManager().getTimeZone());
        } else { 
            cal = Calendar.getInstance(getXMLConversionManager().getTimeZone(), loc);
        } 
        cal.setTimeInMillis(dur.getTimeInMillis(cal));
        cal.set(Calendar.YEAR, dur.getYears());
        cal.set(Calendar.MONTH, dur.getMonths() - 1);
        cal.set(Calendar.DATE, dur.getDays());
        cal.set(Calendar.HOUR_OF_DAY, dur.getHours());
        cal.set(Calendar.MINUTE, dur.getMinutes());
        cal.set(Calendar.SECOND, dur.getSeconds());
        return cal;
    } 
     
    /** 
     * Convert from a String representation of an SDO date type to a Calendar 
     * using the default locale. Same as toCalendar(dateString, null). 
     *  
     * @param dateString 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:negate
  } 
 
  @Override 
  public Duration negate()
  { 
    return duration.negate();
  } 
 
  @Override 
  public Duration normalizeWith(Calendar startTimeInstant)
  { 
    return duration.normalizeWith(startTimeInstant);
  } 
 
  private void writeObject(ObjectOutputStream out) throws IOException
  { 
    out.writeUTF(toString());
  } 
 
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
3
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
        try { 
            DatatypeFactory df = DatatypeFactory.newInstance();
            // my work number in milliseconds: 
            Duration myPhone = df.newDuration(9054133519l);
            Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
            int compareVal = myPhone.compare(myLife);
            switch (compareVal) {
                case DatatypeConstants.LESSER:
                    System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
                    break; 
                case DatatypeConstants.EQUAL:
                    System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
                    break; 
                case DatatypeConstants.GREATER:
                    System.out.println("There are more milliseconds in my phone number than my lifespan.");
                    break; 
                case DatatypeConstants.INDETERMINATE:
                    System.out.println("The comparison could not be carried out.");
            } 
             

5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:compare
        try { 
            DatatypeFactory df = DatatypeFactory.newInstance();
            // my work number in milliseconds: 
            Duration myPhone = df.newDuration(9054133519l);
            Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
            int compareVal = myPhone.compare(myLife);
            switch (compareVal) {
                case DatatypeConstants.LESSER:
                    System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
                    break; 
                case DatatypeConstants.EQUAL:
                    System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
                    break; 
                case DatatypeConstants.GREATER:
                    System.out.println("There are more milliseconds in my phone number than my lifespan.");
                    break; 
                case DatatypeConstants.INDETERMINATE:
                    System.out.println("The comparison could not be carried out.");
            } 
             

4
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:getTimeInMillis
  public static void main(String arg[]) throws Exception {
 
    DatatypeFactory factory = DatatypeFactory.newInstance();
 
    Duration d = factory.newDuration("P3D");
    long secs1 = d.getTimeInMillis(new Date()) / 1000;
    Calendar c = Calendar.getInstance();
    c.set(1970, 0, 1, 0, 0, 0);
    long secs2 = d.getTimeInMillis(c.getTime()) / 1000;
 
    System.out.printf("%d %d same = %s%n", secs1, secs2, secs1 == secs2);
  } 
} 
4
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:addTo
  } 
 
  private Date add(Date date, Duration duration) {
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    duration.addTo(calendar);
    return calendar.getTime();
  } 
 
  private Date parseDate(String date) throws Exception {
      return datatypeFactory.newXMLGregorianCalendar(date).toGregorianCalendar().getTime();
  } 
 
  private Duration parsePeriod(String period) throws Exception {
      return datatypeFactory.newDuration(period);
  } 
 
  private boolean isDuration(String time) {
    return time.startsWith("P");
  } 
4
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Java 8 Duration Questions & Answers:

See Code Examples for Java 8 Duration Methods: