Date类
/*
* 时间,日期和数字
* Date类
* Date类存在于java.util包中,Date类的无参构造方法创建的对象可以获取本地当前的时间。
* Date对象表示时间的一般顺序为:星期、月、日、小时、分、秒、年
* 计算机时间的公元设置在1970年1月1日0时(格林威治时间)
* 格式化时间必须用到java.text.SimpleDateFormat包
* 格式化格式:年份yyyy或者yy 月份M或MM如果想用汉语输出月份,则至少要三个MMM d或者dd输出日 H或HH输出小时 m或mm输出分 s或ss输出秒
* E表示用字符串输出星期
*/
import java.util.*;
import java.text.SimpleDateFormat;//如果要格式化日期,则必须调用
public class DateClass_Date {
public static void main(String[] args){
Date nowTime=new Date();//无参构造函数获取当前时间,输出格式为Sat Jun 09 15:22:13 CST 2012
System.out.println(nowTime);
SimpleDateFormat matter1=new SimpleDateFormat("'当前时间为':yyyy年MM月dd日HH时mm分ss秒");
System.out.println(matter1.format(nowTime));//time:2012年06月09日15时24分32秒
SimpleDateFormat matter2=new SimpleDateFormat("北京时间:yyyy年MM月dd日HH时mm分ss秒");
System.out.println(matter2.format(nowTime));
Date date1=new Date(1000);
Date date2=new Date(-1000);
System.out.println(matter1.format(date1));
System.out.println(matter1.format(date2));//如果时间早于1970年1月1日0时,则可以通过这种方法获取以前的时间
}
}
Calendar类
/*
* new和getInstance()的区别,new一定要生成一个新对象,分配内存。getInstance则不一定需要再次创建,他可以吧一个已经存在引用
* 让你使用,性能上由于new
* new创建后只能当次使用,而getinstance则可以跨栈区使用,或者远程跨区域使用,getInstance通常是创建static静态实例的方法
* Calendar在java.util包中
*
*/
import java.util.*;
public class DateClass_Calendar {
public static void main(String[] args){
Calendar calendar=Calendar.getInstance();//用getInstance()来初始化对象,一般为静态对象
calendar.setTime(new Date());//获取当前时间的日历
String year=String.valueOf(calendar.get(Calendar.YEAR));
String month=String.valueOf(calendar.get(Calendar.MONTH)+1);//月份需加1
String day=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
String week=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1);//星期需减1
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int second=calendar.get(Calendar.SECOND);
System.out.println("现在的时间是:");
System.out.println(""+year+"年"+month+"月"+day+"日"+"星期"+week);
System.out.println(""+hour+"时"+minute+"分"+second+"秒");
calendar.set(2012,9,0);//将日历翻到2006年9月11
long time20060911=calendar.getTimeInMillis();//将20060911表示为毫秒
calendar.set(2012,6,8);//将日历翻到19625月29
long time19620529=calendar.getTimeInMillis();
long days=(time20060911-time19620529)/(1000*60*60*24);
System.out.println("相隔多少天:"+days);
}
}
根据Calendar类和date类做的一个计算你从出生到现在的天数的小程序
/*
* @version 出生天数查看器 v1.0
* @author sevend
* 利用Calendar类做的一个计算从出生到现在多少天的小工具
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class DateClass_CalendarGame extends JFrame implements ActionListener{
JLabel lblNote;
JLabel lblYear;
JLabel lblMonth;
JLabel lblDay;
JLabel lblResult;
JLabel lblNowTime;
JTextField txtYear;
JTextField txtMonth;
JTextField txtDay;
JButton jbtnSubmit;
Calendar calendar=Calendar.getInstance();
int nowYear;
int nowMonth;
int nowDay;
int birthYear;
int birthMonth;
int birthDay;
long nowTime=0;
long birthTime=0;
long result;
DateClass_CalendarGame(){
this.setTitle("出生天数查看器");
lblNote=new JLabel("请输入您的出生日期:");
lblYear=new JLabel("年份(xxxx):");
lblMonth=new JLabel("月份(xx):");
lblDay=new JLabel("天(xx):");
lblResult=new JLabel();
lblNowTime=new JLabel();
txtYear=new JTextField(10);
txtMonth=new JTextField(10);
txtDay=new JTextField(10);
jbtnSubmit=new JButton("查看");
jbtnSubmit.addActionListener(this);
Container c=getContentPane();
c.setLayout(null);
c.add(lblDay);
c.add(lblMonth);
c.add(lblYear);
c.add(lblNote);
c.add(lblResult);
c.add(lblNowTime);
c.add(txtDay);
c.add(txtMonth);
c.add(txtYear);
c.add(jbtnSubmit);
lblNote.setBounds(40,20,200,40);
lblYear.setBounds(40,70,200,40);
txtYear.setBounds(250,70,60,40);
lblMonth.setBounds(40,110,200,40);
txtMonth.setBounds(250,110,60,40);
lblDay.setBounds(40,150,200,40);
txtDay.setBounds(250,150,60,40);
lblResult.setBounds(40,200,200,40);
jbtnSubmit.setBounds(40,250,100,40);
lblNowTime.setBounds(40,300,250,40);
this.setBounds(200,60,500,400);
this.setVisible(true);
this.setResizable(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ew){
dispose();
}
});
this.calendar.setTime(new Date());
this.nowYear=calendar.get(Calendar.YEAR);
this.nowMonth=calendar.get(Calendar.MONTH)+1;
this.nowDay=calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(nowYear,nowMonth,nowDay);
nowTime=calendar.getTimeInMillis();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtnSubmit){
if(txtYear.getText().trim().length()!=0&&txtMonth.getText().trim().length()!=0&&
txtDay.getText().trim().length()!=0){
this.calendar.set(Integer.parseInt(txtYear.getText()), Integer.parseInt(txtMonth.getText()),
Integer.parseInt(txtDay.getText()));
this.birthTime=calendar.getTimeInMillis();
this.result=(nowTime-birthTime)/(1000*60*60*24);
lblNowTime.setText("当前:"+this.nowYear+this.nowMonth+this.nowDay);
lblResult.setText("您从出生到现在已经有"+this.result+"天了");
}else{
lblNowTime.setText("请输入完整信息!");
}
}
}
public static void main(String[] args){
new DateClass_CalendarGame();
}
}
仿作的一个在网页上经常看到的月历和当前时间的小功能
/*
* 当前时间和本月月历
*/
import java.util.*;
import java.text.SimpleDateFormat;
public class DateClass_CalendarNowTime {
public static void main(String[] args){
System.out.println("当前时间为:");
Date date=new Date();
SimpleDateFormat patter=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
System.out.println(patter.format(date));
Calendar calendar=Calendar.getInstance();
int nowMonth=calendar.get(Calendar.MONTH);
int nowYear=calendar.get(Calendar.YEAR);
calendar.set(nowYear, nowMonth,1);
System.out.println("本月月历");
System.out.println(" 日 一 二 三 四 五 六");
int week=calendar.get(Calendar.DAY_OF_WEEK)-1;
String[] a=new String[week+31];
for(int i=0;i<week;i++){
a[i]="**";
}
for(int i=week,n=1;i<week+31;i++){
if(n<=9)
a[i]=String.valueOf(n)+" ";
else
a[i]=String.valueOf(n);
n++;
}
for(int i=0;i<a.length;i++){
if(i%7==0){
System.out.println("");
}
System.out.print(" "+a[i]);
}
}
}
分享到:
相关推荐
postgresql中判断是否为数字和是否为时间日期格式的相关函数,类似于sqlserver中的isDate和isnumeric函数
本文档讲解了在struts2中格式化日期、时间和数字
日期时间 及 数字的 格式化 参数 大全
VB 中 Format 格式化日期时间、数字函数功能详解,详情包含有: 数字类型的格式化 日期类型的格式化 文本类型的格式化 强制使用中文格式的日期时间 总结:这是一份不可多得的珍贵珍藏版资源!!!
将****-**-** *:**:**格式的时间转换成数字,由此可实现对时间先后发生顺序的判断等诸多操作。本例只以年月日小时这一部分做了处理,若要细化到分秒,思路皆是如此。
mysql时间转成数字-mysql日期转换和数字格式转换.pdf
java将数字转换为时间格式,好方法,可以看看
PChangeTime 数字时间和标准时间的转换工具 日常需要转换时间
可以修改Office文件数字签名有效日期和部分EXE安装文件 *本程序仅供测试,请勿用于非法用途!
java 将excel里面的数字时间转化为准确的时间格式;具体来源有点忘记了,实现是摘自了开源代码里面的一段。
vb.net(月历、日期时间、数字)
C#_数字格式日期时间格式枚举格式[ToString(给不同参数)].
数字用法和日期时间表示法.pptx
EL表达式的一些操作。包括字符串处理,时间日期的格式化,数字处理
使用js实现的获取当前详细日期时间信息的数字时钟 html 源代码
f 完整日期和时间(长日期和短时间) F FullDateTimePattern(长日期和长时间) g 常规(短日期和短时间) G 常规(短日期和长时间) m、M MonthDayPattern r、R RFC1123Pattern s 使用当地时间的 ...
C# 常用日期时间函数--DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.DateTim--DateTime 数字型 System.DateTime currentTime=new ...
第六章时间日期数字预习笔记.pdf