java 年 月 日 的取值问题总结

1.    年,月,日的取值问题

 

/****************************方案一*************************************/

package com.test.test;

import java.text.DateFormat;

import java.util.Calendar;

import java.util.Date;

public class TestDate2 {

   public void getTimeByDate(){

       Date date = new Date();

       System.out.println("小时="+date.getHours());

       DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日

       System.out.println(df1.format(date));

       DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒

       System.out.println(df2.format(date));

        DateFormat df3 =DateFormat.getTimeInstance();//只显示出时分秒

       System.out.println(df3.format(date));

       DateFormat df4 =DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)

       System.out.println(df4.format(date));

       DateFormat df5 =DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期,上下午,时间(精确到秒)

       System.out.println(df5.format(date));

       DateFormat df6 =DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,上下午,时间(精确到分)

       System.out.println(df6.format(date));

       DateFormat df7 =DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)

       System.out.println(df7.format(date));

    }

   public void getTimeByCalendar(){

       Calendar cal = Calendar.getInstance();

       int year = cal.get(Calendar.YEAR);//获取年份

       int month=cal.get(Calendar.MONTH)+1;//获取月份

       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)-1;//一周的第几天

       System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒       星期"+WeekOfYear);

    }

   public static void main(String[] args) {

             TestDate2 t=new TestDate2();

       t.getTimeByDate();

       System.out.println("****************************");

       t.getTimeByCalendar();

    }

}

/****************************方案*************************************/

package com.test.test;

 

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

public class TestDate3 { 

   public static void main(String[] args) { 

        // 获取当前年份、月份、日期 

       Calendar cale = null; 

       cale = Calendar.getInstance(); 

       int year = cale.get(Calendar.YEAR); 

       int month = cale.get(Calendar.MONTH) + 1; 

       int day = cale.get(Calendar.DATE); 

       int hour = cale.get(Calendar.HOUR_OF_DAY); 

       int minute = cale.get(Calendar.MINUTE); 

       int second = cale.get(Calendar.SECOND); 

       int dow = cale.get(Calendar.DAY_OF_WEEK); 

       int dom = cale.get(Calendar.DAY_OF_MONTH);  

       int doy = cale.get(Calendar.DAY_OF_YEAR); 

 

       System.out.println("Current Date: " + cale.getTime()); 

       System.out.println("Year: " + year); 

       System.out.println("Month: " + month); 

       System.out.println("Day: " + day); 

       System.out.println("Hour: " + hour); 

       System.out.println("Minute: " + minute); 

       System.out.println("Second: " + second); 

       System.out.println("Day of Week: " + dow); 

       System.out.println("Day of Month: " + dom); 

       System.out.println("Day of Year: " + doy); 

 

       // 获取当月第一天和最后一天 

       SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd"); 

       String firstday, lastday; 

       // 获取前月的第一天 

       cale = Calendar.getInstance(); 

       cale.add(Calendar.MONTH, 0); 

       cale.set(Calendar.DAY_OF_MONTH, 1); 

       firstday = format.format(cale.getTime()); 

       // 获取前月的最后一天 

       cale = Calendar.getInstance(); 

       cale.add(Calendar.MONTH, 1); 

       cale.set(Calendar.DAY_OF_MONTH, 0); 

       lastday = format.format(cale.getTime()); 

       System.out.println("本月第一天和最后一天分别是: " + firstday + " and " + lastday); 

 

       // 获取当前日期字符串 

       Date d = new Date(); 

       System.out.println("当前日期字符串1:" + format.format(d)); 

       System.out.println("当前日期字符串2:" + year + "/" + month + "/" + day + "" 

                + hour + ":" + minute+ ":" + second); 

   } 

 

 

/****************************方案*************************************/

package com.test.test;

import java.util.Calendar;

import java.util.Date;

public class TestDate4 {

          public static int getCurrentMonthDay() { 

                 Calendar a =Calendar.getInstance(); 

                 a.set(Calendar.DATE, 1); 

                 a.roll(Calendar.DATE, -1); 

                 int maxDate =a.get(Calendar.DATE); 

                 return maxDate; 

             } 

        

          public static int getDaysByYearMonth(int year,int month) { 

                 Calendar a =Calendar.getInstance(); 

                 //a.set(Calendar.YEAR, year); 

                 a.set(Calendar.MONTH, month - 1); 

                 a.set(Calendar.DATE, 1); 

                 a.roll(Calendar.DATE, -1); 

                 int maxDate =a.get(Calendar.DATE); 

                 return maxDate; 

             } 

         publicstatic void main(String args[]){

                   Datedate=new Date();

                   TestDate4test=new TestDate4();

                   inta=test.getCurrentMonthDay();

                   date.getDate();

                   date.getMonth();

                   //intb=test.getDaysByYearMonth(2);

                   System.out.println(date.getDate());

                   System.out.println(date.getMonth());

         }

 

}

 

/****************************方案*************************************/

         //获取某月多少天

         public  int getDaysByYearMonth( int month) {        

       Calendar a = Calendar.getInstance(); 

       //a.set(Calendar.YEAR, year); 

       a.set(Calendar.MONTH, month - 1); 

       a.set(Calendar.DATE, 1); 

       a.roll(Calendar.DATE, -1); 

       int maxDate = a.get(Calendar.DATE); 

       return maxDate; 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值