java中CalendargetInstance()和new Date()的差别如下:
CalendargetInstance()是获取一个Calendar对象并可以进行时间的计算,时区的指定
new Date()是创建了一个date对象,默认是utc格式的。
二者可以相互转化:
Calendar calendar = CalendargetInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendargetTime();
// 将 Date 对象反应到一个 Calendar 对象中,
// Calendar/GregorianCalendar 没有构造函数可以接受 Date 对象
// 所以我们必需先获得一个实例,然后设置 Date 对象
calendarsetTime(date);
万年历代码:
/
@(#)AidyCalenderjava
@author
@version 100 2008/7/19
/
import javaawt;
import javaawtevent;
import javalangStringBuffer;
import javaxswing;
import javautil;
import javaxswingTimer;
import javaxswingborder;
public class AidyCalender extends JFrame implements ActionListener,ItemListener{
Date date = new Date();
private GregorianCalendar gregorianCalendar = new GregorianCalendar();
//定义中英文字符数组存储星期信息,用于转换显示
private String[] stringWeekEn = new String[] { "SUN", "MON", "TUE", "WED",
"THU", "FRI", "SAT" };
private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六" };
//定义存储月份的信息数组,用于转换显示方示
private String[] stringMonthEn = new String[] { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
private String[] stringMonthCn = {"一月","二月","三月","四月","五月","六月",
"七月","八月","九月","十月","十一月","十二月"};
private String[] sysNowTime = new String[6];//sysNowTime 用于存储系统时间的变量
private String[] sysRunTime = new String[6];
private JLabel []labelWeek = new JLabel[7];
private JLabel []labelDay = new JLabel[42];
private JLabel labelTime = new JLabel();
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JComboBox combo1 = new JComboBox();
private JComboBox combo2 = new JComboBox();
private JButton buttonToday = new JButton();
private Border border = BorderFactorycreateRaisedBevelBorder();
private Border border1 = BorderFactorycreateLineBorder(Colorcyan,3);
public AidyCalender(String title) {
super(title);
for (int y = 1900; y < 2101; y++) {
combo1addItem(" " + new Integer(y)toString()+"年");
}
for (int m = 0;m<12;m++){
combo2addItem(" "+stringMonthCn[m]);
}
buttonTodaysetText("今 天");
setLayout(new FlowLayout());
add(panel1);
add(panel2);
add(panel3);
panel1setLayout(new GridLayout(1,3,10,0));
panel1add(combo1);
combo1addItemListener(this);
panel1add(combo2);
combo2addItemListener(this);
panel1add(buttonToday);
buttonTodayaddActionListener(this);
labelTimesetFont(new Font("宋体",FontPLAIN,16));
labelTimesetForeground(ColorMAGENTA);
panel1add(labelTime);
Timer time = new Timer(1000,new TimerListener());
timeaddActionListener(new TimerListener());
//timesetRepeats(true);
timestart();
//labelTimeaddAncestorListener(new TimerListener());
panel2setLayout(new GridLayout(7,7,0,10));
panel2setBackground(Colorwhite);
for(int i=0;i<7;i++){
labelWeek[i] = new JLabel();
labelWeek[i]setHorizontalAlignment(0);
if(i==0||i==6){
labelWeek[i]setBackground(Colorblue);
labelWeek[i]setForeground(ColorRED);
labelWeek[i]setFont(new Font("黑体",FontBOLD,14));
}
else{
labelWeek[i]setForeground(ColorBLACK);
labelWeek[i]setFont(new Font("新宋体",FontPLAIN,14));
}
labelWeek[i]setText(stringWeekCn[i]);
panel2add(labelWeek[i]);
}
for(int i= 0;i<42;i++){
labelDay[i] = new JLabel();
labelDay[i]setHorizontalAlignment(0);
labelDay[i]setText("");
panel2add(labelDay[i]);
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Systemexit(0);
}
});
setSize(300,300);
setBounds(250, 200, 400, 360);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
getSysDate();
setNowDate();
}
public void actionPerformed(ActionEvent ae){
if(aegetSource()==buttonToday){
setNowDate();
}
}
public void itemStateChanged(ItemEvent aa){
setChangeDate();
}
public int turnWeek(String week){
int i;
for(i=0;i<7;i++)
if(weekequalsIgnoreCase(stringWeekEn[i]))
break;
return i;
}
public int turnMonth(String month){
/
int turnMonth(String month)
@month 系统日期中的月,诸如Jan\Feb
@return int
返回一个整数值,用于寻找stringMonthCn[]数组中对应的中文月份
/
int i;
for(i=0;i<12;i++)
if(monthequalsIgnoreCase(stringMonthEn[i]))
break;
return i;
}
/
setNowDate()
设置当前系统日期
/
public void setNowDate(){
setSysDate(getNowYear(),getNowMonth());
getSysRunDate();
setDateNull();
combo1setSelectedIndex(getShowYear() - 1900);
combo2setSelectedIndex(getShowMonth());
setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysRunTime[0]),getNowDay());
//labelTimesetText(sysNowTime[3]);
//labelTimesetHorizontalAlignment(0);
}
/Integer getShowYear()
获取组合框中应该显示的年份
/
public void setSysDate(int year,int month){
gregorianCalendarset(year,month,1);
}
public void setDateNull(){
for(int i=0;i<42;i++){
labelDay[i]setText("");
}
}
public void setChangeDate(){
setSysDate(getComboYear(),getComboMonth());
getSysRunDate();
setDateNull();
setDays(getMonthDays(getComboYear(),getComboMonth()),getInitWeek(sysRunTime[0]),-1);
}
public int getMonthDays(int year, int month) {
/
返回所选年月的天数,因为数组中的数值从0开始,所以3\5\8\10分别代表4\6\9\11几个小月
而1代表2月,经过是否为闰年判断,选择返回28或29天
其余月份为大月,返回31天
/
switch (month) {
case 3:
case 5:
case 8:
case 10:
return 30;//小月返回30天
case 1:
if (gregorianCalendarisLeapYear(year)) {
//isLeapYear(year)确定当前纪元中的指定年份是否为闰年。
return 29;
} else {
return 28;
}//闰年的二月返回29天,平年返回28天
default:
return 31;
//大月返回31天
}
}
/
int getComboYear()
获取组合框中的年份
/
public void getSysDate(){
date = gregorianCalendargetTime();
sysNowTime = (datetoString())split(" ");
}
public void getSysRunDate(){
date = gregorianCalendargetTime();
sysRunTime = (datetoString())split(" ");
}
public int getComboYear(){
return combo1getSelectedIndex()+1900;
}
/
int getComboMonth()
获取月组合框中的整数值,
/
public int getComboMonth(){
return combo2getSelectedIndex();
}
public int getInitWeek(String initWeek){
/
getWeekNow(String initWeek)
@para nowWeek 系统日期中的星期
返回当月中的1号是从星期几开始
/
int nowWeek = 0 ;
for(int i = 0;i<7;i++){
if(initWeekequalsIgnoreCase(stringWeekEn[i])){
nowWeek = i;
break;
}
}
return nowWeek;
}
public int getNowYear(){
return IntegerparseInt(sysNowTime[5]);
}
public int getNowMonth(){
int nowMonth=0;
for(int i=0;i<12;i++){
if(sysNowTime[1]equalsIgnoreCase(stringMonthEn[i]));
nowMonth=i;
break;
}
return nowMonth;
}
public int getNowDay(){
return IntegerparseInt(sysNowTime[2]);
}
public Integer getShowYear(){
return IntegerparseInt(sysNowTime[5]);
}
public Integer getShowMonth(){
/
Integer getShowMonth()
获取在组给框中显示的中文格式月份:如七月\八月等
/
return turnMonth(sysNowTime[1]);
}
public void setDays(int monthDays,int initWeek,int day){
/
void setDays(int monthDays,int initWeek,int day)
@para monthDays 本月天数
@para initWeek 初始星期
@para day 今天日
设置月历
/
setDateNull();
for(int i=initWeek;i<initWeek+monthDays+1;i++){
if((i-initWeek+1)==day){
labelDay[i]setBorder(border1);
labelDay[i]setForeground(ColorBLUE);
labelDay[i]setFont(new Font("黑体",FontBOLD,20));
}else if((i%7==0)||(i%7==6))
labelDay[i]setForeground(ColorRED);
else{
labelDay[i]setForeground(ColorBLACK);
}
labelDay[i]setText(StringvalueOf(i-initWeek+1));
}
for(int i=initWeek+monthDays;i<42;i++)
labelDay[i]setText("");
}
class TimerListener implements ActionListener{
//AdapterDemo var=new AdapterDemo("万年历程序--Aidy");
public void actionPerformed(ActionEvent e) {
GregorianCalendar g = new GregorianCalendar();
String clock = new String((ggetTime()toString()split(" "))[3]);
labelTimesetText(clock);
}
}
public static void main(String args[])
{
try{
UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
AidyCalender var=new AidyCalender("万年历程序--Aidy");
}
}
记事本代码这里已经贴不上了。你百度HI我,我给你。
Date类型并没有格式,只有转换成String格式的时候让格式化显示。
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());
Calendar calendar = CalendargetInstance();
int year = IntegerparseInt(datetimesubstring(0,4));
int month = IntegerparseInt(datetimesubstring(5,7));
int date = IntegerparseInt(datetimesubstring(8,10));
int hour = IntegerparseInt(datetimesubstring(11,13));
int minute = IntegerparseInt(datetimesubstring(14,16));
//int second = IntegerparseInt(datetimesubstring(17,19));
if(calendarget(CalendarYEAR)>year){
int y = calendarget(CalendarYEAR)-year;
扩展资料:
Date类可以在javautil包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),创建一个表示创建时刻的对象。getTime()方法返回Date对象的long值。
import javautil;
public class Now {
public static void main(String[] args) {
Date now = new Date();
long nowLong = nowgetTime();
Systemoutprintln("Value is " + nowLong);
-java日期函数
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:
我想计算2个算法程序运行的时间,做一下比较
JAVA中有那些计算时间函数?最好以毫秒显示出来。
我知道SystemcurrentTimeMillis()
但是不知道为什么,几个程序运行的时间都一样,都是16ms,也不知道是我程序太小了
还是写的不对,大家还知道别的计算时间的函数没?最好在程序开始计算,到程序最后
结束,最后显示一下时间。
解析:
import javautil;
public class RunningTime
{
public static void main(String argc[]) throws InterruptedException
{
int i=0,j=0;
long k=0,begin,end,time;
Date mydate=new Date();
begin=mydategetTime();
for (;i<100;i++)
{
for(;j<100;j++)
{
ThreadcurrentThread()sleep(5);
k+=ij;
}
}
Date mydate2=new Date();
end=mydate2getTime();
time=end-begin;
Systemoutprintln(begin+"运行时间为:"+time+" "+end);
}
}
计算运算时间的程序
应该这样写
Calendar nowTime = CalendargetInstance();
Date nowDate = (Date) nowTimegetTime();
Calendar afterTime = CalendargetInstance();
afterTimeadd(CalendarMONTH, 5);
Date afterDate = (Date) afterTimegetTime();
我想说得是 java已经不推荐使用date类了,所以尽量用Calendar 来实现
public class DateCalcu {
public static String getYesterday(String Today) {
String Yes = "";
String year = "", month = "", day = "";
int y = 0, m = 0, d = 0;
year = Todaysubstring(0, 4);
month = Todaysubstring(4, 6);
day = Todaysubstring(6, 8);
y = IntegerparseInt(year);
m = IntegerparseInt(month);
d = IntegerparseInt(day);
d = d - 1;
if (d == 0) {
m = m - 1;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10) {
d = 31;
}
if (m == 2)
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) // 如果是闰年
d = 29;
else
d = 28;
else if (m != 0)
d = 30;
}
if (m == 0) {
y = y - 1;
m = 12;
d = 31;
}
year = StringvalueOf(y);
if (m < 10)
month = "0" + StringvalueOf(m);
else
month = StringvalueOf(m);
if (d < 10)
day = "0" + StringvalueOf(d);
else
day = StringvalueOf(d);
Yes = year + month + day;
return Yes;
}
public static void main(String args[]) {
String yesterday = "20090301";
Systemoutprintln("今天是" + yesterday);
yesterday = getYesterday(yesterday);
Systemoutprintln("昨天是" + yesterday);
}
}
////////
//看看符合不符合你的要求
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)