Java 获取当前时间

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值