• JAVA对时间的操作


    1.Date获取当前时间

      1.1将时间毫秒转为日期格式。

     import java.sql.Date;
    Date d = new Date(System.currentTimeMillis());//传当前的毫秒时间
     String time=d.toLocaleString();//返回2015-8-17 11:08:26格式字符串
    
    //使用SimpleDateFormat转换需要的格式
    import java.text.SimpleDateFormat;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
    System.out.println(sdf.format(longtime)); //输出2015-8-17 11:08:26

    1.2获取时间示例

    public void getTimeByDate(){
    Date date = new Date();
    DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
    System.out.println(df1.format(date));
    DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
    System.out.println(df2.format(date));
    DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
    System.out.println(df3.format(date));
    DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒) 
    System.out.println(df4.format(date));  
    DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期,上下午,时间(精确到秒) 
    
    System.out.println(df5.format(date));
    DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,上下午,时间(精确到分) 
    System.out.println(df6.format(date));
    DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
    System.out.println(df7.format(date));
    
      public void getTimeByCalendar(){
    Calendar cal = Calendar.getInstance()
    int year = cal.get(Calendar.YEAR);//获取年份
    int month=cal.get(Calendar.MONTH);//获取月份 
    int day=cal.get(Calendar.DATE);//获取日 
    int hour=cal.get(Calendar.HOUR);//小时 
    int minute=cal.get(Calendar.MINUTE);//
    int second=cal.get(Calendar.SECOND);//
    int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
    System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒星期"+WeekOfYear);
                }

    待续。。。

  • 相关阅读:
    微信菜单设置为小程序报错85005错误
    VS2013常用快捷键
    VS2017专业版和企业版激活密钥
    微信小程序image组件binderror使用例子(对应html、js中的onerror)
    本科无学位有哪些途径来获得硕士学位
    asp.net 微信JsSDK
    使用nginx反向代理,一个80端口下,配置多个微信项目
    对称与非对称加密;SSL;HTTPS;AJP
    java 在实例化异常的时候做的事情
    简单理解正向,反向,透明代理
  • 原文地址:https://www.cnblogs.com/freemanabc/p/5493064.html
Copyright © 2020-2023  润新知