工具篇——DateUtil(用于获取时间)

版权声明:旨在技术交流与成长,欢迎大家来交流学习。 https://blog.csdn.net/qq941263013/article/details/82841540

写代码的四点:

     1.明确需求。要做什么?

     2.分析思路。要怎么做?(1,2,3……)

     3.确定步骤。每一个思路要用到哪些语句、方法和对象。

     4.代码实现。用具体的语言代码将思路实现出来。

学习新技术的四点:

     1.该技术是什么?

     2.该技术有什么特点?(使用需注意的方面)

     3.该技术怎么使用?(写Demo)

     4.该技术什么时候用?(在Project中的使用场景 )

----------------------早计划,早准备,早完成。-------------------------

代码如下:

package com.wy.test.other;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间的工具类
 * 1.获取系统当前时间(年月日 时分秒)(12小时制);
 * 2.获取系统当前时间(年月日 时分秒)(24小时制);
 * 3.系统当前时间(年月日)
 */

public class DateUtil {

    /**
     * 获取系统当前时间(12小时制)
     * 区别:12小时制使用小写的hh,24小时制使用大写的HH
     * @return  系统当前时间(年月日 时分秒)
     */
    public static String getCurrent12DateTotal() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        String date = simpleDateFormat.format(new Date());
        return date;
    }

    /**
     * 获取系统当前时间(24小时制)
     * 区别:12小时制使用小写的hh,24小时制使用大写的HH
     * @return  系统当前时间(年月日 时分秒)
     */
    public static String getCurrent24DateTotal() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        String date = simpleDateFormat.format(new Date());
        return date;
    }

    /**
     * 获取系统当前时间
     * @return  系统当前时间(年月日)
     */
    public static String getCurrentDateHalf() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        return date;
    }
}

在项目中的应用:

        //12小时制年月日时分秒
        String current12DateTotal = DateUtil.getCurrent12DateTotal();
        //24小时制年月日时分秒
        String current24DateTotal = DateUtil.getCurrent24DateTotal();
        //年月日
        String currentDateHalf = DateUtil.getCurrentDateHalf();

猜你喜欢

转载自blog.csdn.net/qq941263013/article/details/82841540