zoukankan      html  css  js  c++  java
  • 【转】java获取当前年、月、日 、小时 、分钟、 秒、 毫秒

    public class Test {
        /**
         * 英文简写(默认)如:2010-12-01
         */
        public static String FORMAT_SHORT = "yyyy-MM-dd";
        /**
         * 英文全称  如:2010-12-01 23:15:06
         */
        public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
        /**
         * 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S
         */
        public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
        /**
         * 中文简写  如:2010年12月01日
         */
        public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
        /**
         * 中文全称  如:2010年12月01日  23时15分06秒
         */
        public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
        /**
         * 精确到毫秒的完整中文时间
         */
        public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";
     
     
        public static void main(String[] args) {
            System.out.println(getTimeString());
            System.out.println("返回日期年份:"+getYear(new Date()));
            System.out.println("返回月份:"+getMonth(new Date()));
            System.out.println("返回当天日份"+getDay(new Date()));
            System.out.println("返回当天小时"+getHour(new Date()));
            System.out.println("返回当天分"+getMinute(new Date()));
            System.out.println("返回当天秒"+getSecond(new Date()));
            System.out.println("返回当天毫秒"+getMillis(new Date()));
     
        }
     
     
     
        /**
         * 获取当前时间
         */
        public static String getTimeString() {
            SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
            Calendar calendar = Calendar.getInstance();
            return df.format(calendar.getTime());
        }
     
        /**
         * 获取日期年份
         * @param date 日期
         * @return
         */
        public static String getYear(Date date) {
            return format(date).substring(0, 4);
        }
        /**
         * 功能描述:返回月
         *
         * @param date
         *            Date 日期
         * @return 返回月份
         */
        public static int getMonth(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.MONTH) + 1;
        }
     
        /**
         * 功能描述:返回日期
         *
         * @param date
         *            Date 日期
         * @return 返回日份
         */
        public static int getDay(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
     
        /**
         * 功能描述:返回小时
         *
         * @param date
         *            日期
         * @return 返回小时
         */
        public static int getHour(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.HOUR_OF_DAY);
        }
     
        /**
         * 功能描述:返回分
         *
         * @param date
         *            日期
         * @return 返回分钟
         */
        public static int getMinute(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.MINUTE);
        }
     
        /**
         * 返回秒钟
         *
         * @param date
         *            Date 日期
         * @return 返回秒钟
         */
        public static int getSecond(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.SECOND);
        }
     
        /**
         * 功能描述:返回毫
         *
         * @param date
         *            日期
         * @return 返回毫
         */
        public static long getMillis(Date date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.getTimeInMillis();
        }
    }

    原文:https://blog.csdn.net/xuforeverlove/article/details/81565173

    //日期字符串类型转date
    public void test() throws ParseException {
            String string = "2016-10-24 21:59:06";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.parse(string));
    }
  • 相关阅读:
    Luogu 1080 【NOIP2012】国王游戏 (贪心,高精度)
    Luogu 1314 【NOIP2011】聪明的质检员 (二分)
    Luogu 1315 【NOIP2011】观光公交 (贪心)
    Luogu 1312 【NOIP2011】玛雅游戏 (搜索)
    Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)
    Luogu 1514 引水入城 (搜索,动态规划)
    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
    Luogu 1437 [HNOI2004]敲砖块 (动态规划)
    Luogu 1941 【NOIP2014】飞扬的小鸟 (动态规划)
    HDU 1176 免费馅饼 (动态规划)
  • 原文地址:https://www.cnblogs.com/licheng0201/p/10669656.html
Copyright © 2011-2022 走看看