zoukankan      html  css  js  c++  java
  • Java各种获取系统当前时间方法和格式

    在学习编程的过程中,我觉得不止要获得课本的知识,更多的是通过学习技术知识提高解决问题的能力,这样我们才能走在最前方,本文主要讲述Java各种获取系统当前时间方法和格式,更多Java专业知识,广州疯狂java培训官网与你分享;

    如何利用JAVA快速准确提取当前系统时间方法和格式,我们先来看实现代码:

        01./**

        02.     * 返回当前日期时间字符串<br>

        03.     * 默认格式:yyyy-mm-dd hh:mm:ss

        04.     *

        05.     * @return String 返回当前字符串型日期时间

        06.     */

        07.    public static String getCurrentTime() {

        08.        String returnStr = null;

        09.        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        10.        Date date = new Date();

        11.        returnStr = f.format(date);

        12.        return returnStr;

        13.    }

        14.

        15.    /**

        16.     * 返回当前日期时间字符串<br>

        17.     * 默认格式:yyyymmddhhmmss

        18.     *

        19.     * @return String 返回当前字符串型日期时间

        20.     */

        21.    public static BigDecimal getCurrentTimeAsNumber() {

        22.        String returnStr = null;

        23.        SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");

        24.        Date date = new Date();

        25.        returnStr = f.format(date);

        26.        return new BigDecimal(returnStr);

        27.    }

        28.

        29.

        30.    /**

        31.     * 返回自定义格式的当前日期时间字符串

        32.     *

        33.     * @param format

        34.     *            格式规则

        35.     * @return String 返回当前字符串型日期时间

        36.     */

        37.    public static String getCurrentTime(String format) {

        38.        String returnStr = null;

        39.        SimpleDateFormat f = new SimpleDateFormat(format);

        40.        Date date = new Date();

        41.        returnStr = f.format(date);

        42.        return returnStr;

        43.    }

        44.

        45.    /**

        46.     * 返回当前字符串型日期

        47.     *

        48.     * @return String 返回的字符串型日期

        49.     */

        50.    public static String getCurDate() {

        51.        Calendar calendar = Calendar.getInstance();

        52.        SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");

        53.        String strDate = simpledateformat.format(calendar.getTime());

        54.        return strDate;

        55.    }

        56.

        57.    /**

        58.     * 返回指定格式的字符型日期

        59.     * @param date

        60.     * @param formatString

        61.     * @return

        62.     */

        63.    public static String Date2String(Date date, String formatString) {

        64.        if (G4Utils.isEmpty(date)) {

        65.            return null;

        66.        }

        67.        SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);

        68.        String strDate = simpledateformat.format(date);

        69.        return strDate;

        70.    }

        71.

        72.    /**

        73.     * 返回当前字符串型日期

        74.     *

        75.     * @param format

        76.     *            格式规则

        77.     *

        78.     * @return String 返回的字符串型日期

        79.     */

        80.    public static String getCurDate(String format) {

        81.        Calendar calendar = Calendar.getInstance();

        82.        SimpleDateFormat simpledateformat = new SimpleDateFormat(format);

        83.        String strDate = simpledateformat.format(calendar.getTime());

        84.        return strDate;

        85.    }

        86.

        87.    /**

        88.     * 返回TimeStamp对象

        89.     *

        90.     * @return

        91.     */

        92.    public static Timestamp getCurrentTimestamp() {

        93.        Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");

        94.        if (obj != null)

        95.            return (Timestamp) obj;

        96.        else

        97.            return null;

        98.    }

        99.

        100.    /**

        101.     * 将字符串型日期转换为日期型

        102.     *

        103.     * @param strDate

        104.     *            字符串型日期

        105.     * @param srcDateFormat

        106.     *            源日期格式

        107.     * @param dstDateFormat

        108.     *            目标日期格式

        109.     * @return Date 返回的util.Date型日期

        110.     */

        111.    public static Date stringToDate(String strDate, String srcDateFormat,

    String dstDateFormat) {

        112.        Date rtDate = null;

        113.        Date tmpDate = (new SimpleDateFormat(srcDateFormat))。parse

    (strDate, new ParsePosition(0));

        114.        String tmpString = null;

        115.        if (tmpDate != null)

        116.            tmpString = (new SimpleDateFormat(dstDateFormat))。format

    (tmpDate);

        117.        if (tmpString != null)

        118.            rtDate = (new SimpleDateFormat(dstDateFormat))。parse

    (tmpString, new ParsePosition(0));

        119.        return rtDate;

        120.    }

    疯狂Java培训专注软件开发培训,提升学员就业能力,重点提升实践动手能力。高薪从IT名企请来项目经理为学员亲自授课,对学员进行实战教学,在没有工作经验的学员,在疯狂java,通过大量全真经典企业级项目进行集中培训,学员通过数月培训都可获得1-2年的工作经验,进而在同类的求职者中脱颖而出。疯狂Java培训让你体会java编程的快乐,项目开发的兴奋,成就感,通过短短几个月的时间,让你在半年的时间内掌握8-10万的代码量,掌握Java核心技术,迅速成为技能型的现代化高端人才,迅速获得高薪就业!

  • 相关阅读:
    bzoj2599
    在Linux下配置jdk的环境变量
    liunx 命令大全
    Liunx下如何使用kettle
    Liunx 解压篇
    Linux下安装MySQL-5.7
    Linux文件权限查看及修改命令chmod,chown
    spring 驱动模式
    Struts2标签之Checkbox
    spring 注解的优点缺点
  • 原文地址:https://www.cnblogs.com/gojava/p/3384594.html
Copyright © 2011-2022 走看看