使用Calendar类set()方法

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();
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值