Java Date gets date and time seconds

Source: Internet
Author: User
Tags dateformat

12345678910111213141516171819202122232425262728293031323334353637383940414243 packagecom.util;importjava.text.DateFormat;importjava.util.Calendar;importjava.util.Date;publicclassTest {    publicvoidgetTimeByDate(){        Date date = newDate();        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 voidgetTimeByCalendar(){        Calendar cal = Calendar.getInstance();        intyear = cal.get(Calendar.YEAR);//获取年份        intmonth=cal.get(Calendar.MONTH);//获取月份        intday=cal.get(Calendar.DATE);//获取日        inthour=cal.get(Calendar.HOUR);//小时        intminute=cal.get(Calendar.MINUTE);//分                   int second=cal.get(Calendar.SECOND);//秒        intWeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天        System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒       星期"+WeekOfYear);    }    publicstaticvoidmain(String[] args) {        Test t=newTest();        t.getTimeByDate();        System.out.println("****************************");        t.getTimeByCalendar();    }}

  

Get day, if greater than 16 + 2 months, otherwise + 1 months, output 7 months

123456789101112131415 publicstaticvoidmain(String[] as) throwsException {    intj;    Calendar cc = Calendar.getInstance();    intmyYear = cc.get(Calendar.YEAR);    intmyMonth = cc.get(Calendar.MONTH);    j=cc.get(Calendar.DATE)>16?3:2;    cc.set(Calendar.MONTH, myMonth+j);    System.out.println(myYear+"-"+cc.get(cc.MONTH));    for(inti=0;i<7;i++){        cc.add(cc.MONTH, 1);        intmm = cc.get(cc.MONTH);        intmmm=mm==0?12:mm;        System.out.println(myYear+"-"+mmm);    }}

  

Here is a blog http://www.blogjava.net/xiaoyi/articles/295044.html, there are some common methods of implementation, you can refer to the optimization of their own

 

Java Date gets date and time seconds

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.