博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.util.Calendar
阅读量:5750 次
发布时间:2019-06-18

本文共 3874 字,大约阅读时间需要 12 分钟。

java.util.Calendar

The java.util.Calendar class is used to represent the date and time. The year, month, day, hour, minute, second, and milliseconds can all be set or obtained from a Calendar object. The default Calendar object has the current time in it. There are also methods for making data calculations.

Other related classes: Date, and DateFormat, ...

To get the current time

The default Calendar constructor produces an object whose fields are set to the current time for the default timezone and locale.

Calendar now = Calendar.getInstance();

 

Getting the value of the fields

The following field names can be used as an argument to the Calendar.get(. . .)method. In all of these examples, tis a Calendar object.

Access Method

Meaning

t.get(Calendar.YEAR)

int value of the year

t.get(Calendar.MONTH)

int value of the month (0-11)

t.get(Calendar.DAY_OF_MONTH)

int value of the day of the month (1-31)

t.get(Calendar.DAY_OF_WEEK)

int value of the day of the week (0-6)

t.get(Calendar.HOUR)

int value of the hour in 12 hour notation (0-12)

t.get(Calendar.AM_PM)

returns either Calendar.AM or Calendar.PM

t.get(Calendar.HOUR_OF_DAY)

int value of the hour of the day in 24-hour notation (0-24)

t.get(Calendar.MINUTE)

int value of the minute in the hour (0-59)

t.get(Calendar.SECOND)

int value of the second within the minute (0-59).

t.get(Calendar.MILLISECOND)

int value of the milliseconds within a second (0-999).

 

Example :

// File   :  animation/textclock/TextClock1.java// Purpose: Show use of Timer, Calendar to implement a clock.// Enhancements: Center text, 12 hour with AM/PM, ....// Author : Fred Swartz, 1999 ... 2007-03-02, Placed in public domainimport java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Calendar; TextClock1class TextClock1 extends JFrame {	    //============================================================== fields    private JTextField _timeField;  // set by timer listener    //========================================================== constructor    public TextClock1() {        //... Set characteristics of text field that shows the time.        _timeField = new JTextField(5);        _timeField.setEditable(false);        _timeField.setFont(new Font("sansserif", Font.PLAIN, 48));        JPanel content = new JPanel();        content.setLayout(new FlowLayout());        content.add(_timeField);                 this.setContentPane(content);        this.setTitle("Text Clock 1");        this.pack();        this.setLocationRelativeTo(null);        this.setResizable(false);        //... Create timer which calls action listener every second..        //    Use full package qualification for javax.swing.Timer        //    to avoid potential conflicts with java.util.Timer.        javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener());        t.start();    }        /// inner class ClockListener    class ClockListener implements ActionListener {    	public void actionPerformed(ActionEvent e) {    		//... Whenever this is called, get the current time and    		//    display it in the textfield.            Calendar now = Calendar.getInstance();            int h = now.get(Calendar.HOUR_OF_DAY);            int m = now.get(Calendar.MINUTE);            int s = now.get(Calendar.SECOND);            _timeField.setText("" + h + ":" + m + ":" + s);            //... The following is an easier way to format the time,            //    but requires knowing how to use the format method.            //_timeField.setText(String.format("%1$tH:%1$tM:%1$tS", now));    	}    }        //================================================================= main    public static void main(String[] args) {        JFrame clock = new TextClock1();        clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        clock.setVisible(true);    }}

转载于:https://www.cnblogs.com/haimingwey/archive/2012/04/16/2451879.html

你可能感兴趣的文章
Android组件化最佳实践 ARetrofit原理
查看>>
舍弃浮躁, 50条重要的C++学习建议
查看>>
同步手绘板——将View的内容映射成Bitmap转图片导出
查看>>
虚拟机安装OS_X_Lion 反复注册问题
查看>>
【Android游戏开发之十】(优化处理)详细剖析Android Traceview 效率检视工具!分析程序运行速度!并讲解两种创建SDcard方式!...
查看>>
微信小程序之wx.navigateback往回携带参数
查看>>
陌陌和请吃饭之类的应用,你要是能玩转,那就厉害了
查看>>
递归的运行机制简单理解
查看>>
汉字转阿斯克马值
查看>>
Java 栈与堆简介
查看>>
【supervisord】部署单进程服务的利器
查看>>
zabbix oracle监控插件orabbix部署安装
查看>>
python3 通过qq 服务器 发送邮件
查看>>
java 多线程踩过的坑
查看>>
ggplot2 geom相关设置—点重合处理(jitter)
查看>>
部署Replica Sets及查看相关配置
查看>>
倒序显示数组(从右往左)
查看>>
LeetCode2_Evaluate Reverse Polish Notation评估逆波兰表达式(栈)
查看>>
文献综述二:UML技术在行业资源平台系统建模中的应用
查看>>
阿里云服务器 linux下载 jdk
查看>>