📄 mydatetime.java
字号:
/**
* @(#)MyDateTime.java
*
*
* @author
* @version 1.00 2009/3/12
*/
public class MyDateTime {
//记录现在是当前年份的第几天.
private static int theDaythOfTheYear=0;
private static int theDaythOfTheMonth=0;
/**
* Creates a new instance of <code>MyDateTime</code>.
*/
public MyDateTime() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//输出当前日期与时间,取格林尼治时间(比北京时间晚8个小时).
System.out.println("Current date is "+getCurrentYear()+"/"+getCurrentMonth()+"/"+getCurrentDay()
+" \nCurrentTime is "+getCurrentHour()+":"+getCurrentMinute()+":"+getCurrentSecond()+" GMT");
}
//判断闰年,是返回TRUE,否则返回FALSE.
public static boolean isLeapYear(int year)
{
return ((year%4==0)&&(year%100!=0))||(year%400==0);
}
//得到指定年份月份的天数
public static int numberOfDaysInMonth(int year,int month)
{
int numberOfDays=0;
switch(month)
{
//注意加上break.否则会出现错误.
case(1):case(3):case (5):case (7):
case (8):case (10):case (12):
numberOfDays=31;
break;
case (4):case (6):case (9):case (11):
numberOfDays=30;
break;
case (2):
//调用刚刚定义的isLeapYear()
numberOfDays=(isLeapYear(year))?29:28;
break;
default:numberOfDays=0;
}
return numberOfDays;
}
//获取总的秒数,起点是1970年1月1日零点整
public static long getTotalSeconds()
{
//根据系统的函数,获得累计微秒数.
long totalSeconds=System.currentTimeMillis()/1000;
return totalSeconds;
}
//得到当前时间的秒
public static int getCurrentSecond()
{
//注意数据类型显性转换
int currentSecond=(int)getTotalSeconds()%60;
return currentSecond;
}
//获取总的分钟数,起点是1970年1月1日零点整
public static long getTotalMinutes()
{
long totalMinutes=getTotalSeconds()/60;
return totalMinutes;
}
//获取当前时间的分
public static int getCurrentMinute()
{
int currentMinute=(int)getTotalMinutes()%60;
return currentMinute;
}
//获取总的小时数
public static long getTotalHours()
{
long totalHours=(int)getTotalMinutes()/60;
return totalHours;
}
//获取当前时间的时
public static int getCurrentHour()
{
int currentHour=(int)getTotalHours()%24;
return currentHour;
}
//获取总的天数
public static int getTotalDays()
{
int totalDays=(int)getTotalHours()/24;
return totalDays;
}
//获取当前的年份
public static int getCurrentYear()
{
//开始年份,1970年
final int beginYear=1970;
//初始化当年年份
int currentYear=beginYear;
//刚开始剩余的天数是起点时间至当前的总天数(不包括今天).
//记录剩余天数可以简化求出当前的日期
int remainDays=getTotalDays();
//求当前年份以及剩余天数
/**剩余天数不大于365时,循环结束.
*注意remainDays始终是从起点截止到昨天(含)的总天数.
*此时currentYear即为当年的年份.
*/
while(remainDays>365)
{
//调用函数判断年份
if(isLeapYear(currentYear))
remainDays-=366;
else
remainDays-=365;
//年份自增
currentYear++;
}
/**此时若剩余天数365时,此时为当前年的第366天(如果有的话),
*如果是闰年,OK;
*如果不是闰年,则需要年份加1,剩余天数减去365.
*/
if(remainDays==365&&!isLeapYear(currentYear))
{
currentYear++;
remainDays-=365;
}
//remainDays是截止到昨天的的总天数,
//因此需要加1,以表示今天是今年的第几天.
theDaythOfTheYear=remainDays+1;
return currentYear;
}
//得到当前的月份
public static int getCurrentMonth()
{
int currentMonth=1;
int year=getCurrentYear();
//注意该语句必须在上述getCurrentYear()调用后,才可以赋值.
//这里是为了减少计算量的,所以使用了这个两个静态变量.
//实际上做好不适用,重新计算,即可(循环实现)
theDaythOfTheMonth=theDaythOfTheYear;
//判断余下的天数是否比这个月份的天数多.
while(theDaythOfTheMonth>numberOfDaysInMonth(year,currentMonth))
{
theDaythOfTheMonth-=numberOfDaysInMonth(year,currentMonth);
currentMonth++;
}
return currentMonth;
}
//得到当前的日期.
public static int getCurrentDay()
{
//获得theDaythOfTheMonth
getCurrentMonth();
return theDaythOfTheMonth;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -