Skip to content

Instantly share code, notes, and snippets.

@alanland
Created June 27, 2013 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanland/5873722 to your computer and use it in GitHub Desktop.
Save alanland/5873722 to your computer and use it in GitHub Desktop.
joda-time 例子
//Joda-Time 的一个小例子,计算一天的工作时间,
//每天工作8小时,1小时休息:
package com.alzhang.workingtime;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Hours;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
import org.joda.time.format.PeriodPrinter;
public class Calculate {
private static String TIME_PATTERN = "M/d/yyyy h:mm:ss a";
private static Hours FORMAL_TIME = Hours.EIGHT;
private static Hours RESET_TIME = Hours.ONE;
private static DateTimeFormatter FORMATTER;
private static PeriodPrinter PRINTER;
static String simpleTime1 = "4/28/2010 8:50:26 AM";
static String simpleTime2 = "4/28/2010 5:35:54 PM";
static {
FORMATTER = DateTimeFormat.forPattern(TIME_PATTERN);
PRINTER = new PeriodFormatterBuilder().appendHours().appendSuffix(
" hour", " hours").appendSeparator(" and ").appendMinutes()
.appendSuffix(" min").appendSeparator(" ").appendSeconds()
.appendSuffix(" second", " seconds").toPrinter();
}
/**
* @param args
*/
public static void main(String[] args) {
DateTime start = FORMATTER.parseDateTime(simpleTime1);
DateTime end = FORMATTER.parseDateTime(simpleTime2);
long worked = new Duration(start, end).minus(FORMAL_TIME.plus(RESET_TIME).toStandardDuration()).getMillis();
StringBuffer sb = new StringBuffer();
if (worked < 0) {
sb.append("Worked not enough! Still need work : ");
PRINTER.printTo(sb, new Period(Math.abs(worked)), null);
} else {
sb.append("Good job! Worked : ");
PRINTER.printTo(sb, new Period(Math.abs(worked)).plusHours(FORMAL_TIME.getHours()), null);
}
System.out.println(sb.toString());
}
}
//修改了下,统计一周的工作时间:
package com.alzhang.workingtime;
import org.apache.commons.lang.StringUtils;
import org.joda.time.Duration;
import org.joda.time.Hours;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
import org.joda.time.format.PeriodPrinter;
public class Calculate {
private static final String TIME_PATTERN = "M/d/yyyy h:mm:ss a";
private static final Hours FORMAL_TIME = Hours.EIGHT;
private static final Hours RESET_TIME = Hours.ONE;
private static final DateTimeFormatter FORMATTER;
private static final PeriodPrinter PRINTER;
private static String[][] times = {
{"5/5/2010 10:53:54 AM", "5/5/2010 6:34:58 PM"},
{"5/4/2010 9:44:58 AM", "5/4/2010 6:59:12 PM"},
{"", ""},
{"", ""},
{"", ""}};
static {
FORMATTER = DateTimeFormat.forPattern(TIME_PATTERN);
PRINTER = new PeriodFormatterBuilder().appendHours().appendSuffix(
" hour", " hours").appendSeparator(" and ").appendMinutes()
.appendSuffix(" min").appendSeparator(" ").appendSeconds()
.appendSuffix(" second", " seconds").toPrinter();
}
/**
* @param args
*/
public static void main(String[] args) {
long worked = 0;
for(String[] time : times)
worked += getWorkedTimeForOneDay(time[0], time[1]);
StringBuffer sb = new StringBuffer();
if (worked < 0) {
sb.append("Worked not enough! Still need work : ");
printTime(new Period(Math.abs(worked)), sb);
} else {
sb.append("Good job! Worked : ");
printTime(new Period(Math.abs(worked)).plusHours(FORMAL_TIME.getHours()), sb);
}
System.out.println(sb.toString());
}
private static void printTime(Period period, StringBuffer sb) {
PRINTER.printTo(sb, period, null);
}
public static long getWorkedTimeForOneDay(String start, String end){
if(StringUtils.isBlank(start) || StringUtils.isBlank(end))
return - FORMAL_TIME.toStandardDuration().getMillis(); // you sucked all day
return new Duration(FORMATTER.parseDateTime(start), FORMATTER.parseDateTime(end)).minus(FORMAL_TIME.plus(RESET_TIME).toStandardDuration()).getMillis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment