java calendar毫秒_java1.8中如何使用精确到毫秒的时间

SimpleDateFormat,用于显示时间的格式。

Date,获取精确到毫秒的时间。

Calendar,获取日历格式的时间。

示例代码如下:

package baseAPI;

import java.util.* ; // 导入需要的工具包

import java.text.* ; // 导入SimpleDateFormat所在的包

class mmm{ // 以后直接通过此类就可以取得日期时间

private SimpleDateFormat sdf = null ; // 声明SimpleDateFormat对象

public String getDate(){ // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS

this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;

return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作

}

public String getDateComplete(){ // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒

this.sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒") ;

return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作

}

public String getTimeStamp(){ // 得到的是一个时间戳

this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;

return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作

}

};

public class useDate{

public static void main(String args[]){

mmm dt = new mmm() ;

System.out.println("系统日期:"+dt.getDate()) ;

System.out.println("中文日期:"+dt.getDateComplete()) ;

System.out.println("时间戳:"+dt.getTimeStamp()) ;

Calendar calendar = new GregorianCalendar();

StringBuffer strb= new StringBuffer();

strb.append(calendar.get(Calendar.YEAR));

strb.append("-"+(calendar.get(Calendar.MONTH)+1));

strb.append("-"+calendar.get(Calendar.DAY_OF_MONTH));

strb.append(" "+calendar.get(Calendar.HOUR_OF_DAY));

strb.append(":" +calendar.get(Calendar.MINUTE));

strb.append(":"+calendar.get(Calendar.SECOND));

strb.append(":"+calendar.get(Calendar.MILLISECOND));

System.out.println(strb);

}

};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值