JAVA-手机日期和时间显示

image

package com.itheima;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ShowDateTime02 {
    public static void main(String[] args) {
        JFrame jf=new JFrame();
        jf.setTitle("猜数字游戏");
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);        //取消窗体的默认布局


        //提示日期
        JLabel dataLable=new JLabel("日期");
        dataLable.setBounds(50,50,100,20);
        jf.add(dataLable);

        Date d=new Date();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日");
        String dateString = sdf.format(d);


        //按照格式显示日期的字符串
        JLabel showDateLable=new JLabel(dateString);
        showDateLable.setBounds(50,80,200,20);
        jf.add(showDateLable);

        //提示时间
        JLabel timeLable = new JLabel("时间");
        timeLable.setBounds(50,150,100,20);
        jf.add(timeLable);

        SimpleDateFormat sdf2=new SimpleDateFormat("HH:mm");
        String timeString = sdf2.format(d);

        //按照格式显示时间的字符串
        JLabel showtimeLable = new JLabel(timeString);
        showtimeLable.setBounds(50,180,200,20);
        jf.add(showtimeLable);










        //添加按钮到窗体中
        jf.setVisible(true);


    }
}

执行结果

![image](https://img2022.cnblogs.com/blog/2426413/202211/2426413-20221106163333004-320099359.png)

posted @ 2022-11-06 16:33  NiceTwocu  阅读(27)  评论(0编辑  收藏  举报