温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java1.8中如何使用时间类精确到毫秒的时间

发布时间:2021-08-31 09:37:18 来源:亿速云 阅读:147 作者:chen 栏目:软件技术

这篇文章主要介绍“java1.8中如何使用时间类精确到毫秒的时间”,在日常操作中,相信很多人在java1.8中如何使用时间类精确到毫秒的时间问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java1.8中如何使用时间类精确到毫秒的时间”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

主要涉及三个类:

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

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

  3. 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);
    }
};

到此,关于“java1.8中如何使用时间类精确到毫秒的时间”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI