Calendar calendar = Calendar.getInstance();//获取Calendar类的实例
calendar.clear();
//.set()方法中 month 范围为0-11 对应1-12月
calendar.set(2004,8,15,9,9,9);
各种时间的操作
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Time_Date extends Frame{ //继承Frame类
Time_Date() // 构造函数
{
//1.创建Frame对象,提供窗体标题:“小应用程序查看器”
Frame f = new Frame("小应用程序查看器");
//2.设置窗体背景
f.setBackground(Color.WHITE);
//3.设置窗体显示大小
f.setSize(500,400);
//4.设置窗体显示位置
f.setLocation(300,300);
//5.设置窗体图标,借用Toolkit类
f.setIconImage(Toolkit.getDefaultToolkit().createImage("生灵.jpg"));
//6.设置窗体,默认是不显示窗体的
f.setVisible(true);
f.setLayout(new FlowLayout());
//使用MenuBar来创建菜单
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("小应用程序");
menuBar.add(menu);
f.setMenuBar(menuBar);
//使用Label添加文本。
// Label label = new Label("Small Application");
// label.setLocation(75,50);
// label.setSize(50,25);
// f.add(label);
//接下来写日期
Date today = new Date();//当前日期和时间
//第一行字符串格式
Label str_label = new Label("字符串格式:" + today.toString());
str_label.setLocation(30,60);
str_label.setSize(300,20);
f.add(str_label);
//第二行系统格式(使用SimpleDateFormat())方法
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-M-dd ah:mm");
Label systime_label = new Label("系统格式:" + simpleDateFormat.format(today));
systime_label.setLocation(30,80);
systime_label.setSize(300,20);
f.add(systime_label);
//第三行调整格式(使用DateFormat.getDateInstance())方法
DateFormat dateFormat = DateFormat.getDateInstance();
Label fixed_label = new Label("调整时间:" + dateFormat.format(today));
fixed_label.setLocation(30,100);
fixed_label.setSize(300,20);
f.add(fixed_label);
//中国格式(使用SimpleDateFormat())方法
SimpleDateFormat simpleDateFormat_China = new SimpleDateFormat("h时mm分ss秒 yyyy年M月dd日");
Label china_time = new Label("中国格式:" + simpleDateFormat_China.format(today));
china_time.setLocation(30,120);
china_time.setSize(300,20);
f.add(china_time);
//调整日期(使用Calendar类)
Calendar calendar = Calendar.getInstance();//获取Calendar类的实例
calendar.clear();
//.set()方法中 month 范围为0-11 对应1-12月
calendar.set(2004,8,15,9,9,9);
today=calendar.getTime();
Label set_Time = new Label("调整后时间:" + today.toString());
set_Time.setLocation(30,140);
set_Time.setSize(300,20);
f.add(set_Time);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("Closing.");
System.exit(0);
}
});
}
public static void main(String args[]) // 主函数
{
Time_Date win = new Time_Date();
}
}