我是靠谱客的博主 曾经皮带,最近开发中收集的这篇文章主要介绍JAVA中Calendar与Date类型互转,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

转载自:https://blog.csdn.net/fz13768884254/article/details/82422752

Java 语言的Date(日期),Calendar(日历),DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分。

Date与String互转详情:https://blog.csdn.net/fz13768884254/article/details/82415432

    (1) Calendar转化为Date  
        Calendar cal=Calendar.getInstance();  
        Date date=cal.getTime();  
          
        (2) Date转化为Calendar  
        Date date=new Date();  
        Calendar cal=Calendar.getInstance();  
        cal.setTime(date);  

Calendar中时间转换方法:

     /**
         * Returns a <code>Date</code> object representing this
         * <code>Calendar</code>'s time value (millisecond offset from the <a
         * href="#Epoch">Epoch</a>").
         *
         * @return a <code>Date</code> representing the time value.
         * @see #setTime(Date)
         * @see #getTimeInMillis()
         */
        public final Date getTime() {
            return new Date(getTimeInMillis());
        }
     
        /**
         * Sets this Calendar's time with the given <code>Date</code>.
         * <p>
         * Note: Calling <code>setTime()</code> with
         * <code>Date(Long.MAX_VALUE)</code> or <code>Date(Long.MIN_VALUE)</code>
         * may yield incorrect field values from <code>get()</code>.
         *
         * @param date the given Date.
         * @see #getTime()
         * @see #setTimeInMillis(long)
         */
        public final void setTime(Date date) {
            setTimeInMillis(date.getTime());
        }
     
        /**
         * Returns this Calendar's time value in milliseconds.
         *
         * @return the current time as UTC milliseconds from the epoch.
         * @see #getTime()
         * @see #setTimeInMillis(long)
         */
        public long getTimeInMillis() {
            if (!isTimeSet) {
                updateTime();
            }
            return time;
        }
     
        /**
         * Sets this Calendar's current time from the given long value.
         *
         * @param millis the new time in UTC milliseconds from the epoch.
         * @see #setTime(Date)
         * @see #getTimeInMillis()
         */
        public void setTimeInMillis(long millis) {
            // If we don't need to recalculate the calendar field values,
            // do nothing.
            if (time == millis && isTimeSet && areFieldsSet && areAllFieldsSet
                && (zone instanceof ZoneInfo) && !((ZoneInfo)zone).isDirty()) {
                return;
            }
            time = millis;
            isTimeSet = true;
            areFieldsSet = false;
            computeFields();
            areAllFieldsSet = areFieldsSet = true;
        }

 

    public void getTimeByCalendar(){
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);//获取年份
            int month=cal.get(Calendar.MONTH);//获取月份
            int day=cal.get(Calendar.DATE);//获取日
            int hour=cal.get(Calendar.HOUR);//小时
            int minute=cal.get(Calendar.MINUTE);//分           
            int second=cal.get(Calendar.SECOND);//秒
            int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
            System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒       星期"+WeekOfYear);
        }

 
---------------------
作者:狂丰
来源:CSDN
原文:https://blog.csdn.net/fz13768884254/article/details/82422752
版权声明:本文为博主原创文章,转载请附上博文链接!

最后

以上就是曾经皮带为你收集整理的JAVA中Calendar与Date类型互转的全部内容,希望文章能够帮你解决JAVA中Calendar与Date类型互转所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部