Java常用类库(日期操作类)

//得到当前系统日期


package org.date;


import java.util.Date;


public class DateDemo01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println("当前日期为:"+date);
}
}


//单独取得系统的当前日期(精确到时分秒)


package org.date;


import java.util.Calendar;
import java.util.GregorianCalendar;


public class DateDemo02 {
public static void main(String[] args) {
Calendar calendar = null;
calendar = new GregorianCalendar();
System.out.println("年:"+calendar.get(Calendar.YEAR));
System.out.println("月:"+(calendar.get(Calendar.MONTH)+1));
System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+calendar.get(Calendar.MINUTE));
System.out.println("秒:"+calendar.get(Calendar.SECOND));
System.out.println("毫秒:"+calendar.get(Calendar.MILLISECOND));
}
}


//DateFormat中的默认操作


package org.date;


import java.text.DateFormat;
import java.util.Date;


public class DateDemo03 {
public static void main(String[] args) {
DateFormat df1 = null;
DateFormat df2 = null;
df1 = DateFormat.getDateInstance();
df2 = DateFormat.getDateTimeInstance();
System.out.println("DATE:"+df1.format(new Date()));
System.out.println("DATETIME:"+df2.format(new Date()));
}
}


//指定日期显示风格


package org.date;


import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;


public class DateDemo04 {
public static void main(String[] args) {
DateFormat df1 = null;
DateFormat df2 = null;
df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,
new Locale("zh","CN"));
df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,
DateFormat.ERA_FIELD,new Locale("zh","CN"));
System.out.println("DATE:"+df1.format(new Date()));
System.out.println("DATETIME:"+df2.format(new Date()));
}
}


//格式化日期操作


package org.date;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateDemo05 {
public static void main(String[] args) {
String strDate = "2014-07-14 12:16:20.567";
String pat1 = "yyyy-mm-dd hh:mm:ss.SSS";
String pat2 = "yyyy年mm月dd日 hh时mm分ss秒SSS毫秒";
SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);
Date d = null;
try {
d = sdf1.parse(strDate);
} catch (ParseException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println(sdf2.format(d));
}
}


//实例操作——取得完整日期

1,实现一:基于Calendar类

package org.date;


import java.util.Calendar;
import java.util.GregorianCalendar;


public class DateTime {
private Calendar calendar = null;
public DateTime(){
this.calendar = new GregorianCalendar();
}
public String getDate(){
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("-");
buf.append(calendar.get(Calendar.MONTH)).append("-");
buf.append(addZero(calendar.get(Calendar.DAY_OF_MONTH)+1,2)).append(" ");
buf.append(addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":");
buf.append(addZero(calendar.get(Calendar.MINUTE),2)).append(":");
buf.append(addZero(calendar.get(Calendar.SECOND),2)).append(".");
buf.append(addZero(calendar.get(Calendar.MILLISECOND),2));
return buf.toString();
}
public String getDateComplete(){
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("年");
buf.append(calendar.get(Calendar.MONTH)).append("月");
buf.append(addZero(calendar.get(Calendar.DAY_OF_MONTH)+1,2)).append("日");
buf.append(addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append("时");
buf.append(addZero(calendar.get(Calendar.MINUTE),2)).append("分");
buf.append(addZero(calendar.get(Calendar.SECOND),2)).append("秒");
buf.append(addZero(calendar.get(Calendar.MILLISECOND),2)).append("毫秒");
return buf.toString();
}
public String addZero(int num,int len){
StringBuffer s = new StringBuffer();
s.append(num);
while(s.length()<len){
s.insert(0, "0");
}
return s.toString();
}
public String getTimeStamp(){
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR));
buf.append(calendar.get(Calendar.MONTH));
buf.append(addZero(calendar.get(Calendar.DAY_OF_MONTH)+1,2));
buf.append(addZero(calendar.get(Calendar.HOUR_OF_DAY),2));
buf.append(addZero(calendar.get(Calendar.MINUTE),2));
buf.append(addZero(calendar.get(Calendar.SECOND),2));
buf.append(addZero(calendar.get(Calendar.MILLISECOND),2));
return buf.toString();
}
}


package org.date;


public class DateDemo06 {
public static void main(String[] args) {
DateTime dt = new DateTime();
System.out.println("系统日期:"+dt.getDate());
System.out.println("中文日期:"+dt.getDateComplete());
System.out.println("时间戳:"+dt.getTimeStamp());
}
}


2,实现二:基于SimpleDateFormat类


package org.date;


import java.text.SimpleDateFormat;
import java.util.Date;


public class DateTime01 {
private SimpleDateFormat sdf = null;
public String getDate(){
this.sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSS");
return sdf.format(new Date());
}
public String getDateComplete(){
this.sdf = new SimpleDateFormat("yyyy年mm月dd日hh时mm分ss秒SSS毫秒");
return sdf.format(new Date());
}
public String getTimeStamp(){
this.sdf = new SimpleDateFormat("yyyymmddhhmmssSSS");
return sdf.format(new Date());
}
}


package org.date;


public class DateDemo07 {
public static void main(String[] args) {
DateTime01 dt = new DateTime01();
System.out.println("系统日期:"+dt.getDate());
System.out.println("中文日期:"+dt.getDateComplete());
System.out.println("系统日期:"+dt.getTimeStamp());
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值