Java Code Examples for org.joda.time.Duration#getStandardSeconds()

The following examples show how to use org.joda.time.Duration#getStandardSeconds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MembershipCardManage.java    From bbs with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
     * 取得下一个订单号
     * 订单号组成(2010年后的年月日时分秒+用户Id后四位+本机Id五位)
     */
    public Long nextNumber(Long userId){
    	//这里是atoNum到MAX_VALUE=99999的时候重新设成0
    	int MAX_VALUE = 99999;
    	number.compareAndSet(MAX_VALUE, 0);

    	DateTime end = new DateTime();  

		//计算区间毫秒数   
		Duration d = new Duration(begin, end);  
//		long hour = d.getStandardHours();//时
		long second = d.getStandardSeconds();//秒  分配的数字最大可以用到2300年
//		long minute = d.getStandardMinutes();//分
		
    	//userId%10000 取后4位
    	return Long.parseLong(second+""+(String.format("%04d", userId%10000))+""+(String.format("%05d", number.incrementAndGet())));
 
    }
 
Example 2
Source File: DateUtils.java    From xmu-2016-MrCode with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 获得两个时间点之间的时间跨度
 * 
 * @param time1
 *            开始的时间点
 * @param time2
 *            结束的时间点
 * @param timeUnit
 *            跨度的时间单位 see {@link JodaTime}
 *            (支持的时间单位有DAY,HOUR,MINUTE,SECOND,MILLI)
 */
public static long lengthBetween(DateTime time1, DateTime time2,
		DurationFieldType timeUnit) {
	Duration duration = Days.daysBetween(time1, time2).toStandardDuration();
	if (timeUnit == JodaTime.DAY) {
		return duration.getStandardDays();
	} else if (timeUnit == JodaTime.HOUR) {
		return duration.getStandardHours();
	} else if (timeUnit == JodaTime.MINUTE) {
		return duration.getStandardMinutes();
	} else if (timeUnit == JodaTime.SECOND) {
		return duration.getStandardSeconds();
	} else if (timeUnit == JodaTime.MILLI) {
		return duration.getMillis();
	} else {
		throw new RuntimeException(
				"TimeUnit not supported except DAY,HOUR,MINUTE,SECOND,MILLI");
	}
}
 
Example 3
Source File: DateUtils.java    From joda-time-android with Apache License 2.0 6 votes vote down vote up
/**
 * Return given duration in a human-friendly format. For example, "4
 * minutes" or "1 second". Returns only largest meaningful unit of time,
 * from seconds up to hours.
 *
 * The longest duration it supports is hours.
 *
 * This method assumes that there are 60 minutes in an hour,
 * 60 seconds in a minute and 1000 milliseconds in a second.
 * All currently supplied chronologies use this definition.
 */
public static CharSequence formatDuration(Context context, ReadableDuration readableDuration) {
    Resources res = context.getResources();
    Duration duration = readableDuration.toDuration();

    final int hours = (int) duration.getStandardHours();
    if (hours != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_hours, hours, hours);
    }

    final int minutes = (int) duration.getStandardMinutes();
    if (minutes != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_minutes, minutes, minutes);
    }

    final int seconds = (int) duration.getStandardSeconds();
    return res.getQuantityString(R.plurals.joda_time_android_duration_seconds, seconds, seconds);
}
 
Example 4
Source File: HelpTypeManage.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
   * 取得下一个Id
   * 商品分类Id组成(2010年后的年月日时分秒+本机Id五位)
   */
  public Long nextNumber(){
  	//这里是atoNum到MAX_VALUE=9999的时候重新设成0
  	int MAX_VALUE = 99999;
  	number.compareAndSet(MAX_VALUE, 0);
  	DateTime end = new DateTime();  
//计算区间毫秒数   
Duration d = new Duration(begin, end);  
long second = d.getStandardSeconds();//秒	
  
  	return Long.parseLong(second+(String.format("%05d", number.incrementAndGet())));
  }
 
Example 5
Source File: QuestionTagManage.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
   * 取得下一个Id
   * 商品分类Id组成(2010年后的年月日时分秒+本机Id五位)
   */
  public Long nextNumber(){
  	//这里是atoNum到MAX_VALUE=9999的时候重新设成0
  	int MAX_VALUE = 99999;
  	number.compareAndSet(MAX_VALUE, 0);
  	DateTime end = new DateTime();  
//计算区间毫秒数   
Duration d = new Duration(begin, end);  
long second = d.getStandardSeconds();//秒	
  
  	return Long.parseLong(second+(String.format("%05d", number.incrementAndGet())));
  }
 
Example 6
Source File: TagManage.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
   * 取得下一个Id
   * 商品分类Id组成(2010年后的年月日时分秒+本机Id五位)
   */
  public Long nextNumber(){
  	//这里是atoNum到MAX_VALUE=9999的时候重新设成0
  	int MAX_VALUE = 99999;
  	number.compareAndSet(MAX_VALUE, 0);
  	DateTime end = new DateTime();  
//计算区间毫秒数   
Duration d = new Duration(begin, end);  
long second = d.getStandardSeconds();//秒	
  
  	return Long.parseLong(second+(String.format("%05d", number.incrementAndGet())));
  }
 
Example 7
Source File: ConfigSupport.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert duration to an representation accepted by Configuration parser.
 * @param duration A duration.
 * @return A StringBuilder with the string representation of the duration.
 */
public static StringBuilder durationConfigString(Duration duration) {
    Duration remainder = duration;

    long days = remainder.getStandardDays();
    remainder = remainder.minus(Duration.standardDays(days));

    long hours = remainder.getStandardHours();
    remainder = remainder.minus(Duration.standardHours(hours));

    long minutes = remainder.getStandardMinutes();
    remainder = remainder.minus(Duration.standardMinutes(minutes));

    long seconds = remainder.getStandardSeconds();
    remainder = remainder.minus(Duration.standardSeconds(seconds));

    if (!remainder.isEqual(Duration.ZERO))
        Logger.getLogger(ConfigSupport.class.getName()).log(Level.WARNING, "Duration is more precise than configuration will handle: {0}, dropping remainder: {1}", new Object[]{duration, remainder});

    StringBuilder result = new StringBuilder();
    if (days != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(days).append('d');
    }
    if (hours != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(hours).append('h');
    }
    if (minutes != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(minutes).append('m');
    }
    if (result.length() == 0 || seconds != 0) {
        if (result.length() != 0) result.append(' ');
        result.append(seconds).append('s');
    }

    return result;
}
 
Example 8
Source File: MedtronicUIPostprocessor.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
private void processTime(MedtronicUITask uiTask) {

        ClockDTO clockDTO = (ClockDTO) uiTask.returnData;

        Duration dur = new Duration(clockDTO.pumpTime.toDateTime(DateTimeZone.UTC),
                clockDTO.localDeviceTime.toDateTime(DateTimeZone.UTC));

        clockDTO.timeDifference = (int) dur.getStandardSeconds();

        MedtronicUtil.setPumpTime(clockDTO);

        if (isLogEnabled())
            LOG.debug("Pump Time: " + clockDTO.localDeviceTime + ", DeviceTime=" + clockDTO.pumpTime + //
                    ", diff: " + dur.getStandardSeconds() + " s");

//        if (dur.getStandardMinutes() >= 10) {
//            if (isLogEnabled())
//                LOG.warn("Pump clock needs update, pump time: " + clockDTO.pumpTime.toString("HH:mm:ss") + " (difference: "
//                        + dur.getStandardSeconds() + " s)");
//            sendNotification(MedtronicNotificationType.PumpWrongTimeUrgent);
//        } else if (dur.getStandardMinutes() >= 4) {
//            if (isLogEnabled())
//                LOG.warn("Pump clock needs update, pump time: " + clockDTO.pumpTime.toString("HH:mm:ss") + " (difference: "
//                        + dur.getStandardSeconds() + " s)");
//            sendNotification(MedtronicNotificationType.PumpWrongTimeNormal);
//        }

    }
 
Example 9
Source File: StackdriverModule.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Provides
static MetricReporter provideMetricReporter(
    MetricWriter metricWriter, @Config("metricsWriteInterval") Duration writeInterval) {
  return new MetricReporter(
      metricWriter,
      writeInterval.getStandardSeconds(),
      new ThreadFactoryBuilder().setDaemon(true).build());
}
 
Example 10
Source File: ImportDatastoreCommand.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Prints dots to console at regular interval while waiting. */
private static void waitInteractively(Duration pollingInterval) throws InterruptedException {
  int sleepSeconds = 2;
  long iterations = (pollingInterval.getStandardSeconds() + sleepSeconds - 1) / sleepSeconds;

  for (int i = 0; i < iterations; i++) {
    TimeUnit.SECONDS.sleep(sleepSeconds);
    System.out.print('.');
    System.out.flush();
  }
}
 
Example 11
Source File: TaskQueueHelper.java    From nomulus with Apache License 2.0 5 votes vote down vote up
public TaskMatcher etaDelta(Duration lowerBound, Duration upperBound) {
  checkState(!lowerBound.isShorterThan(Duration.ZERO), "lowerBound must be non-negative");
  checkState(
      upperBound.isLongerThan(lowerBound), "upperBound must be greater than lowerBound");
  expected.etaDeltaLowerBound = lowerBound.getStandardSeconds();
  expected.etaDeltaUpperBound = upperBound.getStandardSeconds();
  return this;
}
 
Example 12
Source File: Output.java    From helios with Apache License 2.0 5 votes vote down vote up
public static String humanDuration(final Duration dur) {
  final Period p = dur.toPeriod().normalizedStandard();

  if (dur.getStandardSeconds() == 0) {
    return "0 seconds";
  } else if (dur.getStandardSeconds() < 60) {
    return format("%d second%s", p.getSeconds(), p.getSeconds() > 1 ? "s" : "");
  } else if (dur.getStandardMinutes() < 60) {
    return format("%d minute%s", p.getMinutes(), p.getMinutes() > 1 ? "s" : "");
  } else if (dur.getStandardHours() < 24) {
    return format("%d hour%s", p.getHours(), p.getHours() > 1 ? "s" : "");
  } else {
    return format("%d day%s", dur.getStandardDays(), dur.getStandardDays() > 1 ? "s" : "");
  }
}
 
Example 13
Source File: IntegerColumnDurationMapper.java    From jadira with Apache License 2.0 5 votes vote down vote up
@Override
public Integer toNonNullValue(Duration value) {
    long longValue = value.getStandardSeconds();
    if (longValue < Integer.MIN_VALUE || longValue > Integer.MAX_VALUE) {
        throw new IllegalStateException(longValue + " cannot be cast to int without changing its value.");
    }
    return (int)longValue;
}
 
Example 14
Source File: LiveShow.java    From zapp with MIT License 4 votes vote down vote up
public float getProgressPercent() {
	Duration showDuration = new Duration(startTime, endTime);
	Duration runningDuration = new Duration(startTime, DateTime.now());
	return (float) runningDuration.getStandardSeconds() / showDuration.getStandardSeconds();
}
 
Example 15
Source File: PluginDateUtils.java    From template-compiler with Apache License 2.0 4 votes vote down vote up
public static void humanizeDate(long instantMs, long baseMs, String tzId, boolean showSeconds, StringBuilder buf) {
  DateTimeZone timeZone = DateTimeZone.forID(tzId);
  int offset = timeZone.getOffset(instantMs);
  Duration delta = new Duration(baseMs - instantMs + offset);

  int days = (int)delta.getStandardDays();
  int years = (int)Math.floor(days / 365.0);
  if (years > 0) {
    humanizeDatePlural(years, DatePartType.YEAR, buf);
    return;
  }
  int months = (int)Math.floor(days / 30.0);
  if (months > 0) {
    humanizeDatePlural(months, DatePartType.MONTH, buf);
    return;
  }
  int weeks = (int)Math.floor(days / 7.0);
  if (weeks > 0) {
    humanizeDatePlural(weeks, DatePartType.WEEK, buf);
    return;
  }
  if (days > 0) {
    humanizeDatePlural(days, DatePartType.DAY, buf);
    return;
  }
  int hours = (int)delta.getStandardHours();
  if (hours > 0) {
    humanizeDatePlural(hours, DatePartType.HOUR, buf);
    return;
  }
  int mins = (int)delta.getStandardMinutes();
  if (mins > 0) {
    humanizeDatePlural(mins, DatePartType.MINUTE, buf);
    return;
  }
  int secs = (int)delta.getStandardSeconds();
  if (showSeconds) {
    humanizeDatePlural(secs, DatePartType.SECOND, buf);
    return;
  }
  buf.append("less than a minute ago");
}
 
Example 16
Source File: Util.java    From actframework with Apache License 2.0 4 votes vote down vote up
static String formatDuration(Duration d) {
    long s = d.getStandardSeconds();
    return String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60);
}