Java 获取当前时间
来源:互联网 发布:.games域名 编辑:程序博客网 时间:2024/04/28 11:37
import javax.swing.JOptionPane;public class TEST { public static void main(String[] args) { long totalMilliseconds = System.currentTimeMillis(); long totalSeconds = (totalMilliseconds / 1000); int currentSecond = (int)(totalSeconds % 60); long totalMinutes = totalSeconds / 60; int currentMinute = (int)(totalMinutes % 60); long totalHours = totalMinutes / 60 + 8; //简单粗暴 int currentHour = (int)(totalHours % 24); int totalDays = (int)(totalHours / 24); if (currentHour > 0) totalDays++; int currentYear = 2000; do { currentYear++; } while (getTotalDaysInYears(currentYear) < totalDays); int totalNumOfDaysInTheYear = totalDays -getTotalDaysInYears(currentYear - 1); int currentMonth = 0; do { currentMonth++; } while (getTotalDaysInMonths(currentYear, currentMonth) < totalNumOfDaysInTheYear); int currentDay = totalNumOfDaysInTheYear - getTotalDaysInMonths(currentYear, currentMonth - 1); String output = "Current date and time is " + currentMonth + "/" + currentDay + "/" + currentYear + " " + currentHour + ":" + + currentMinute + ":" + currentSecond + " GMT"; JOptionPane.showMessageDialog(null, output); } /** Get the total number of days from Jan 1, 1970 to the specified year*/ static int getTotalDaysInYears(int year) { int total = 0; // Get the total days from 1970 to the specified year for (int i = 1970; i <= year; i++) if (isLeapYear(i)) total = total + 366; else total = total + 365; return total; } /** Get the total number of days from Jan 1 to the month in the year*/ static int getTotalDaysInMonths(int year, int month) { int total = 0; // Add days from Jan to the month for (int i = 1; i <= month; i++) total = total + getNumOfDaysInMonth(year, i); return total; } /** Get the number of days in a month */ static int getNumOfDaysInMonth(int year, int month) { if (month == 1 || month==3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31; if (month == 4 || month == 6 || month == 9 || month == 11) return 30; if (month == 2) if (isLeapYear(year)) return 29; else return 28; return 0; // If month is incorrect. } /** Determine if it is a leap year */ static boolean isLeapYear(int year) { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) return true; return false; }}
- 代码很长但原理简单,就是运行出来时间慢了8个小时,搜索了下,有修改注册表的,有加时区的,有修改myeclipse的。。。。在这简单粗暴的直接加上8小时~
- 根据不重复造轮子“理论”,找到了如下方法:
import java.util.Date;public class TEST3 { public static void main(String[] args) { System.out.println("java.runtime.version:\t"+System.getProperty("java.runtime.version")); System.out.println("当前时间:\t\t"+new Date()); } }
- 上述方法在JDK 1.7下能正常运行
0 0
- Java获取当前时间
- Java获取当前时间
- java 获取当前时间
- java获取当前时间
- Java获取当前时间
- Java获取当前时间.
- Java 获取当前时间
- java获取当前时间
- Java获取当前时间
- Java获取当前时间
- Java获取当前时间
- Java获取当前时间
- Java获取当前时间
- java获取当前时间
- java获取当前时间
- JAVA获取当前时间
- Java获取当前时间
- Java获取当前时间
- 错误集棉
- Dijkstra最短路
- nodejs后台文件上传模块connect-multiparty
- 个人头像上传
- 安装pillow
- Java 获取当前时间
- 2.反向传导算法
- 1102. Invert a Binary Tree (25)【二叉树】——PAT (Advanced Level) Practise
- Qt学习笔记:QMenuBar()、QMenu()和QAction()的使用
- 如何用java实现使用电子邮件控制你的电脑
- Android并发编程之Handler,MessageQueue,Looper详解
- Xilinx petalinux 2015.4 试用
- 顺序表应用1:多余元素删除之移位算法
- 理解js中的闭包