• Date Math SimpleDateFormat 类


      Date  和 SimpleDateFormat

     1     
     2     /*
     3          Date 日期类  许多方法都被Calendar取代了
     4          Date() 获取当前时间  使用概率最高
     5          Calendar 类用常量获取当前时间   一般很少用
     6     
     7         SimpleDateFormat 类 对时间进行格式化 format方法
     8         
     9         String 字符串 转换成时间要利用 parse方法
    10      */
    11     public static void main(String[] args) {
    12         Calendar calendar  = Calendar.getInstance(); //获取当前的系统时间,代替DATE获取时间
    13         //国外设置月份是从零开始的,所以得加一
    14         System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+ 1 )+"月"+calendar.get(Calendar.DATE)); //年月日时间
    15         
    16         //计算时分秒
    17         System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY));
    18         System.out.println("分:"+calendar.get(Calendar.MINUTE));
    19         System.out.println("秒"+calendar.get(Calendar.SECOND));
    20         
    21         
    22         /*
    23             SimpleDateFormat 类 对时间进行格式化
    24                  作用1: 可以把日期转换成指定格式的字符串        format()
    25                  作用2: 可以把一个字符转换成对应的日期        parse()   
    26          */
    27         Date date = new Date();
    28         SimpleDateFormat dateFormat = new SimpleDateFormat();//默认构造方法,没有指定格式
    29         System.out.println(dateFormat.format(date));
    30         
    31         //格式化后对应的日期
    32         SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    33         System.out.println(dateFormat2.format(date));
    34         
    35         SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy年MM月dd日");
    36         String birthday = "2000年11月1日";
    37         try {
    38             //这个字符串格式要和dateformat 中的格式要一致
    39             Date date2 =  dateFormat3.parse(birthday);
    40             System.out.println(date2);
    41         } catch (ParseException e) {
    42             // TODO Auto-generated catch block
    43             e.printStackTrace();
    44         }
    45         
    46         
    47     }

      Math类

     1     /*
     2          Math 类是数学公式类
     3               abs(double a)  是绝对值类
     4               ceil(double a)   向上取整
     5               floor(double a)  向下取整
     6               round()             四舍五入
     7               random()         随机数   0 - 1  可以乘倍数取整
     8                   当然也可以使用Random类
     9               
    10               
    11      */
    12     public static void main(String[] args) {
    13             
    14         int a = -1;
    15         System.out.println(Math.abs(a));
    16         
    17         float b = 3.14f;//取值为大的!
    18         float c = -3.14f;//取值为大的!
    19         System.out.println(Math.ceil(b));
    20         System.out.println(Math.ceil(c));
    21         
    22         System.out.println(Math.floor(b));
    23         System.out.println(Math.floor(c));
    24         
    25         System.out.println("四舍五入: " + Math.round(3.53));
    26         
    27         System.out.println(Math.random());
    28         
    29         Random random = new Random();//产生 0 - 10是随机数
    30         int ran  = random.nextInt(10)+1;  // 就是大于一
    31         System.err.println(ran);
    32         
    33         
    34     
    35     }

      Math

     1     /*
     2          Math 类是数学公式类
     3               abs(double a)  是绝对值类
     4               ceil(double a)   向上取整
     5               floor(double a)  向下取整
     6               round()             四舍五入
     7               random()         随机数   0 - 1  可以乘倍数取整
     8                   当然也可以使用Random类
     9               
    10               
    11      */
    12     public static void main(String[] args) {
    13             
    14         int a = -1;
    15         System.out.println(Math.abs(a));
    16         
    17         float b = 3.14f;//取值为大的!
    18         float c = -3.14f;//取值为大的!
    19         System.out.println(Math.ceil(b));
    20         System.out.println(Math.ceil(c));
    21         
    22         System.out.println(Math.floor(b));
    23         System.out.println(Math.floor(c));
    24         
    25         System.out.println("四舍五入: " + Math.round(3.53));
    26         
    27         System.out.println(Math.random());
    28         
    29         Random random = new Random();//产生 0 - 10是随机数
    30         int ran  = random.nextInt(9)+1;  // 就是大于一
    31         System.err.println(ran);
    32         
    33         
    34         
    35         
    36     
    37     }
  • 相关阅读:
    JQuery -- this 和 $(this) 的区别
    js动态生成表格
    Sublime 3 如何使用列编辑模式
    新版Sublime text3注册码被移除的解决办法
    Sublime text 3 格式化HTML/css/js/json代码 插件
    mingw64 构建 Geos
    C++调用Python浅析
    linux下挂载VHD等虚拟磁盘文件
    linux 下Qt WebEngine 程序打包简单记录
    C++ UTF8和UTF16互转代码
  • 原文地址:https://www.cnblogs.com/bequt/p/5649618.html
Copyright © 2020-2023  润新知