Procesamiento de fechas en java


  • Ejemplo de código de formato de fecha y hora actual del sistema :
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);
    }
}
  • Cadena de fecha a tipo de fecha
    Ejemplo de código:
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);
    }
}

  • Código de tiempo de muestra:
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);
        }
    }
}
  • Crear objeto de fecha en milisegundos
    Ejemplo de código:
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);
    }
}

  • Ejemplo de código de formato de número :
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 de alta precisión BigDecimal
    pertenece a big data, con una precisión extremadamente alta. No es un tipo de datos básico, sino un objeto java (tipo de datos de referencia) Esta es una clase proporcionada por SUN. Usado específicamente en software financiero.
    Ejemplo de código:
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
    }
}

  • Ejemplo de código de número aleatorio :
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);
    }
}
  • Genere cinco números aleatorios no repetidos
    Ejemplo de código:
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;
    }

}

Supongo que te gusta

Origin www.cnblogs.com/yu011/p/12693859.html
Recomendado
Clasificación