📄 datetime.java
字号:
/********************************************************************
* 项目名称 :<b>j2me学习 J2me Wap Explorer</b> <br/>
*
* Copyright 2005-2006 Wuhua. All rights reserved </br>
*
* 本程序只用于学习目的,不能用于商业目的。如有需要请联系作者
********************************************************************/
package org.wuhua.wap.util;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* <b>类名:DateTime.java</b> </br> 编写日期: 2006-6-23 <br/> 程序功能描述:日期时间的工具类 <br/>
* Demo: <br/> Bug: <br/>
*
* 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/>
*
* @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
*/
public final class DateTime {
private static String[] WEEKDAYS_EN = { "SUN", "MON", "TUE", "WED", "THU",
"FRI", "SAT" };
private static String[] WEEKDAYS_CH = { "周日", "周一", "周二", "周三", "周四", "周五",
"周六" };
public final String timeZone;
public final int year;
public final int month;
public int day;
public int weekday;
public final int hour;
public final int minute;
public final int second;
public final int millsecond;
Calendar c;
public DateTime(Date date, String timeZone) {
this.timeZone = timeZone;
c = timeZone == null ? Calendar.getInstance() : Calendar
.getInstance(TimeZone.getDefault());
c.setTime(date);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
weekday = c.get(Calendar.DAY_OF_WEEK);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
second = c.get(Calendar.SECOND);
millsecond = c.get(Calendar.MILLISECOND);
}
public DateTime(long time, String timeZone) {
this(new Date(time), timeZone);
}
public DateTime() {
this(System.currentTimeMillis(), "GMT + 16");
}
public static String beforeOneDate() {
return new DateTime(System.currentTimeMillis() - 24 * 3600 * 1000,
"GMT+8").toDateString();
}
public Date toDate() {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
c.set(Calendar.MILLISECOND, millsecond);
return c.getTime();
}
public String toDateString() {
if (timeZone.equals("GMT-8"))
return (month + 1) + "月" + day + "日" + " ["
+ WEEKDAYS_EN[weekday - 1] + "] ";
else
return (month + 1) + "月" + day + "日" + " ["
+ WEEKDAYS_CH[weekday - 1] + "] ";
}
public void setDate(int day) {
this.day = day;
this.c.set(Calendar.DAY_OF_WEEK, day);
// day = c.get(Calendar.DAY_OF_WEEK);
weekday = c.get(Calendar.DAY_OF_WEEK);
}
public String toTimeString() {
return hour + ":" + minute + ":" + second + ":" + millsecond;
}
public String toString() {
return toDateString() + " " + toTimeString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -