zoukankan      html  css  js  c++  java
  • java中对日期的处理

    • 系统当前时间及日期格式化
      代码示例:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Demo{
        public static void main(String[] args) {
            /*系统当前时间*/
            Date date = new Date();
            System.out.println(date);
            /*SimpleDateFormat专门负责日期格式化。
            * yyyy 年(年是4位
            * MM 月(月是2位)
            * dd 日
            * HH 时
            * mm 分
            * ss 秒
            * SSS 毫秒(毫秒3位,最高999。1000毫秒代表一秒)
            * 注意:在日期格式中,
            * 除了y M d H m s S这些字符不能随便写之外,
            * 剩下的符号格式自己随意组织。*/
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            //SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
            String nowTimeStr = sdf.format(date);
            System.out.println(nowTimeStr);
        }
    }
    
    • 日期字符串转Date类型
      1、Date parse(String source)
      从给定字符串的开始解析文本以生成日期。
      2、代码示例:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DemoTest{
        public static void main(String[] args) throws Exception{
            String time = "2020-04-07 22:55:11 745";
            //格式要一致
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            Date dateTime = sdf.parse(time);
            System.out.println(dateTime);
        }
    }
    
    • 获取时间
      代码示例:
    public class Demo{
        public static void main(String[] args) {
            /*获取自1970年1月1日00:00:00 000到当前系统时间的总毫秒数。*/
            long nowTimeNillis = System.currentTimeMillis();
            System.out.println(nowTimeNillis);
            /*统计一个方法耗时:
            * 调用目标方法之前记录一个毫秒数
            * 在执行完目标方法之后记录一个毫秒数*/
            long begin = System.currentTimeMillis();
            print();
            long end = System.currentTimeMillis();
            System.out.println("耗费时长:" + (end-begin) + "毫秒");
        }
    
        private static void print(){
            for (int i = 0; i < 1000; i++) {
                System.out.println("i = " + i);
            }
        }
    }
    
    • 通过毫秒创建Date对象
      代码示例:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Demo{
        public static void main(String[] args) {
            /*这个时间是1970-01-01 00:00:00 001
            * 参数是1毫秒(起点)*/
            Date time = new Date(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss SSS");
            String strTime = sdf.format(time);
            /*北京是东八区,差八个小时*/
            System.out.println(strTime);//1970-01-01 08:00 001
    
            /*获取昨天的此时的时间*/
            Date time2 = new Date(System.currentTimeMillis() - 1000*60*60*24);
            String strTime2 = sdf.format(time2);
            System.out.println(strTime2);
        }
    }
    
    • 数字格式化
      代码示例:
    import java.text.DecimalFormat;
    
    public class Demo{
        public static void main(String[] args) {
            /*专门负责数字格式化。
            * 数字格式有哪些?
            * # 代表任意数字
            * ,代表千分位
            * . 代表小数点
            * 0 代表不够时补0*/
            DecimalFormat df = new DecimalFormat("###,###.##");
            String s = df.format(1234.567);
            System.out.println(s);//1,234.57
    
            DecimalFormat df2 = new DecimalFormat("###,###.0000");
            String s2 = df2.format(1234.567);
            System.out.println(s2);//1,234.5670
        }
    }
    
    • 高精度BigDecimal
      BigDecimal属于大数据,精度极高。不属于基本数据类型,属于java对象(引用数据类型)这是SUN提供的一个类。专门用在财务软件当中。
      代码示例:
    import java.math.BigDecimal;
    
    public class Demo{
        public static void main(String[] args) {
            BigDecimal v1 = new BigDecimal(832.2112);
            BigDecimal v2 = new BigDecimal(32.1234);
            BigDecimal v3 = v1.multiply(v2) ;
            System.out.println(v3);
            //输出:26733.4532620799959863
            // 378899171949698136469211733
            // 480099563164955611682671587
            // 9142284393310546875
        }
    }
    
    • 随机数
      代码示例:
    import java.util.Random;
    
    public class Demo{
        public static void main(String[] args) {
            /*创建随机数对象*/
            Random random = new Random();
            /*随机产生一个int类型取值范围内的数字。*/
            int num = random.nextInt();
            System.out.println(num);
            /*产生[0~100]之间的随机数。
            * 不能产生101
            * nextInt翻译为:下一个int类型的数据是101,表示只能取到100。*/
            int num2 = random.nextInt(101);
            System.out.println(num2);
        }
    }
    
    • 生成五个不重复的随机数
      代码示例:
    import java.util.Random;
    
    public class Demo{
        public static void main(String[] args) {
            Random random = new Random();
            int[] a = new int[5];
            for (int s = 0; s < a.length; s++) {
                a[s] = -1;
            }
    
            int index = 0;
            while (index < a.length){
                int num = random.nextInt(21);
                if (judge(a, num)){
                    a[index++] = num;
                }
            }
    
    
            for (int k = 0; k < a.length; k++) {
                System.out.println(a[k]);
            }
        }
    
        public static boolean judge(int[] arr, int key){
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] == key){
                    return false;
                }
            }
            return true;
        }
    
    }
    
  • 相关阅读:
    POJ 1061
    LightOJ 1104
    扩展欧几里得算法
    2015 HUAS Summer Trainning #5 E
    2015 HUAS Summer Trainning #5 C
    2015 HUAS Summer Trainning #5 B
    2015 HUAS Summer Trainning #5 A
    2015 HUAS Summer Trainning #4 D
    2015 HUAS Summer Trainning #4 C
    2015 HUAS Summer Trainning #4 B
  • 原文地址:https://www.cnblogs.com/yu011/p/12693859.html
Copyright © 2011-2022 走看看