随手写的一个例子, 只是练习下自己的代码布局以及思路.. 1. 先写下简单的测试 2. 根据常用的不用修改的变量抽取出来, 作为常量(常量的命名可能有点不规范,谅解~) 3. 方法的作用不一样, 抽取出来做不一样的动作. 4. 好久没写代码了, 生了... 最近在弄js什么的, 醉了~ 慢慢来吧 ... 就是一个简单的例子,能用就拿走... 5. 菜鸟代码, 不喜勿喷
/**
* 生命计算
*
* @author Toolo
* @date 2015-07-24
*/
public class Life {
public static DateFormat dfmt =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"
);
public static final int INT_CONSTANT_SECONDS = 1000
;
public static final int INT_CONSTANT_POINTS = 60
;
public static final double INT_CONSTANT_HOUR = 60.0
;
public static final double INT_CONSTANT_DAY = 24.0
;
public static final double INT_CONSTANT_YEAR = 365.0
;
public static final double INT_CONSTANT_MONTH = 12.0
;
private static long difference;
/**
* 方法说明: testMain
*
* @author Toolo
* @date 2015-07-24
* @param args
*/
public static void main(String[] args) {
try {
difference = getTimeDifference("1992-10-22 22:31:40"
);
} catch (ParseException e) {
System.err.println("时间格式转换错误!"
);
e.printStackTrace();
}
long second =
getTimeSecond(difference);
long points =
getTimePoints(second);
double hour =
getTimeHour(points);
double day =
getTimeDay(hour);
double year =
getTimeYear(day);
double month =
getTimeMonth(year);
System.out.println("我在这个世界上生活了[" + second + "]秒"
);
System.out.println("我在这个世界上生活了[" + points + "]分"
);
System.out.println("我在这个世界上生活了[" + hour + "]时"
);
System.out.println("我在这个世界上生活了[" + day + "]天"
);
System.out.println("我在这个世界上生活了[" + month + "]月"
);
System.out.println("我在这个世界上生活了[" + year + "]年"
);
}
/**
* 方法说明: 获取时间差的的月
*
* @author Toolo
* @date 2015-07-24
* @param day
* @return
*/
private static double getTimeMonth(
double year) {
return year *
INT_CONSTANT_MONTH;
}
/**
* 方法说明: 获取时间差的的年
*
* @author Toolo
* @date 2015-07-24
* @param day
* @return
*/
private static double getTimeYear(
double day) {
return day /
INT_CONSTANT_YEAR;
}
/**
* 方法说明: 获取时间差的的天数
*
* @author Toolo
* @date 2015-07-24
* @param hour
* @return
*/
private static double getTimeDay(
double hour) {
return hour /
INT_CONSTANT_DAY;
}
/**
* 方法说明: 获取时间差的的小时
*
* @author Toolo
* @date 2015-07-24
* @param hour
* @return
*/
private static double getTimeHour(
double hour) {
return hour /
INT_CONSTANT_HOUR;
}
/**
* 方法说明: 获取时间差的的分钟
*
* @author Toolo
* @date 2015-07-24
* @param second
* @return
*/
private static long getTimePoints(
long second) {
return second /
INT_CONSTANT_POINTS;
}
/**
* 方法说明: 获取时间差的秒数
*
* @author Toolo
* @date 2015-07-24
* @param difference
* @return
*/
private static long getTimeSecond(
long difference) {
return difference /
INT_CONSTANT_SECONDS;
}
/**
* 方法说明: 获取时间差
*
* @author Toolo
* @date 2015-07-24
* @param longTime
* 要计算的时间
* @return long 时间
* @throws ParseException
*/
private static long getTimeDifference(String longTime) throws ParseException {
Calendar calendar =
Calendar.getInstance();
Date d1 =
dfmt.parse(longTime);
long nowTime =
System.currentTimeMillis();
calendar.setTimeInMillis(nowTime);
System.err.println("现在的北京时间是[" + calendar.getTime() + "]整"
);
System.err.println("你要计算的时间是[" + d1 + "](阳历)"
);
return nowTime -
d1.getTime();
}
}
转载于:https://www.cnblogs.com/Toolo/p/4672153.html
相关资源:数据结构—成绩单生成器