Java gets the time in various formats, gets the date of yesterday and tomorrow, and gets the start and end time of the day

1. Get the current date and time

1. Use Date and DateFormat     

Date now = new Date();

      DateFormat df1 = DateFormat.getDateInstance(); //格式化后的时间格式:2016-2-19 

      String str1 = d1.format(now);

      DateFormat d2 = DateFormat.getDateTimeInstance();//格式化后的时间格式:2016-2-19 20:54:53

      String str2 = d2.format(now);

      DateFormat d3 = DateFormat.getTimeInstance();//格式化后的时间格式:20:54:53

      String str3 = d3.format(now);

      DateFormat d4 = DateFormat.getInstance(); //格式化后的时间格式:16-2-29 下午8:54

      String str4 = d4.format(now);

      DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);//格式化后的时间格式:2016年2月19日 星期五 下午08时54分53秒 CST

      String str5 = d5.format(now);

      DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);//格式化后的时间格式:2016年2月19日 下午08时54分53秒

      String str6 = d6.format(now);

      DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);//格式化后的时间格式:16-2-19 下午8:54

      String str7 = d7.format(now);

      DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化后的时间格式:2016-2-19 20:54:53

      String str8 = d8.format(now);

2. Use canlinder      

Calendar c = Calendar.getInstance();

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

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

      int day=c.get(Calendar.DATE);//获取日

      int minute=c.get(Calendar.MINUTE);//分

      int hour=c.get(Calendar.HOUR);//小时

      int second=c.get(Calendar.SECOND);//秒

      int WeekOfYear = c.get(Calendar.DAY_OF_WEEK);//显示当前日期是一周的第几天,周一就是1,周五就是5

      String date=year +"年"+ month +"月"+ day + "日";//格式:2016年2月19日

      String time=hour +"时"+ minute +"分"+ second +"秒";//格式:8时54分53秒

      String date1=c.getTime()//格式:Fri Feb 19 20:54:53 CST 2016

3. Use Date and SimpleDateFormat (advantage: 24 hours)     

 Date date = new Date();

      SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      String sDateSuffix = dateformat.format(date);

 

2. Get the date of yesterday and tomorrow     

Date date=new Date();//取时间

      Calendar calendar = new GregorianCalendar();

      calendar.setTime(date);

      calendar.add(calendar.DATE,-1);//把日期往前减少一天,若想把日期向后推一天则将负数改为正数

      date=calendar.getTime(); 

     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

     String dateString = formatter.format(date);

3. Get the start time and end time of the day     

Date date=new Date();//取时间
     date.clearTime()
     Calendar calendar = new GregorianCalendar();
     calendar.setTime(date);
     calendar.set(Calendar.HOUR,0)
     calendar.set(Calendar.MINUTE,0)
     calendar.set(Calendar.SECOND,0)
     calendar.set(Calendar.MILLISECOND,0)
     System.out.println("开始时间:"+calendar.getTime())
     calendar.set(Calendar.HOUR,23)
     calendar.set(Calendar.MINUTE,59)
     calendar.set(Calendar.SECOND,59)
     calendar.set(Calendar.MILLISECOND,999)
     System.out.println("结束时间:"+calendar.getTime())

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325210109&siteId=291194637