Introduction to Java Programming编程题5.33<显示当前日期和时间>
来源:互联网 发布:淘宝女鞋2016新款上市 编辑:程序博客网 时间:2024/04/29 06:59
/*Current data and time is August 18, 2015 6:8:23.*/public class CurrentTime { public static void main(String[] args) { showTime(); } public static void showTime() { final long PER_SECONDS_DAY = 60 * 60 * 24; long totalSeconds = (System.currentTimeMillis() + 8 * 60 * 60 * 1000) / 1000; long totaldays = totalSeconds / PER_SECONDS_DAY; System.out.print("Current data and time is "); showCurrentData(totaldays); showCurrentTime(totalSeconds); } public static void showCurrentTime(long totalSeconds) { long seconds = totalSeconds % 60; long minutes = totalSeconds / 60 % 60; long hours = totalSeconds / 60 / 60 % 24; System.out.println(" " + hours + ":" + minutes + ":" + seconds + "."); } public static boolean isLeapYear(long year) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) return true; else return false; } public static void showCurrentData(long totaldays) { final int START_YEAR = 1970; int days, currentYear; for (currentYear = START_YEAR; totaldays >= 365; currentYear++) { if (isLeapYear(currentYear)) days = 366; else days = 365; totaldays -= days; } int month = 1; int n = perMonthDay(currentYear, month); while (totaldays > n) { totaldays -= n; month++; n = perMonthDay(currentYear, month); } totaldays++; System.out.print(toMonthLetter(month) + " " + totaldays + ", " + currentYear); } public static String toMonthLetter(int month) { String monthLetter = ""; switch (month) { case 1: monthLetter = "January"; break; case 2: monthLetter = "February"; break; case 3: monthLetter = "March"; break; case 4: monthLetter = "April"; break; case 5: monthLetter = "May"; break; case 6: monthLetter = "June"; break; case 7: monthLetter = "July"; break; case 8: monthLetter = "August"; break; case 9: monthLetter = "September"; break; case 10: monthLetter = "October"; break; case 11: monthLetter = "November"; break; case 12: monthLetter = "December"; break; } return monthLetter; } public static int perMonthDay(int currentYear, int month) { int n; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: n = 31; break; case 2: if (month == 2) { if (isLeapYear(currentYear)) n = 29; else n = 28; break; } default: n = 30; } return n; }}
0 0
- Introduction to Java Programming编程题5.33<显示当前日期和时间>
- introduction to Java Programming 编程题5.4<反向显示一个整数>
- Introduction to Java Programming编程题7.11<硬币游戏:显示正反面>
- introduction to Java Programming 编程题5.3<回文整数>
- Introduction to Java Programming编程题5.6<打印数字三角形>
- Introduction to Java Programming编程题5.13<数列求和>
- Introduction to Java Programming编程题5.14<计算数列>
- Introduction to Java Programming编程题5.17<01矩阵>
- Introduction to Java Programming编程题5.26<回文素数>
- Introduction to Java Programming编程题5.27<反素数>
- Introduction to Java Programming编程题5.29<掷骰子游戏>
- Introduction to Java Programming编程题5.28<梅森素数>
- Introduction to Java Programming编程题5.30<双素数>
- Introduction to Java Programming编程题9.1<检查SSN>
- Introduction to Java Programming编程题9.2<检查子串>
- Introduction to Java Programming编程题7.6<两个矩阵相乘>
- Introduction to Java Programming编程题9.3<检验密码>
- Introduction to Java Programming编程题9.8<二进制转十进制>
- 我怎么能确保non-corrupt 文件传输 in linux
- SQL server 在自增长的字段中插入指定值
- [LeetCode]Simplify Path
- DeveloperResource
- scikit-learn 学习笔记-1-加载文本语料库
- Introduction to Java Programming编程题5.33<显示当前日期和时间>
- 九度oj 1092
- Linux下Redis3.0.3的部署和启动笔记
- poj-3678(2-SAT)
- js将html table导成excel表格,IE、Google Chrome都能用
- Version和Build字段的关系
- POJ 3259:Wormholes 【SPFA】
- redis常用命令、常见错误、配置技巧等分享
- Introduction to Java Programming编程题6.3<计算数字的出现次数>