码迷,mamicode.com
首页 > 编程语言 > 详细

java常见日期格式转换以及日期的获取

时间:2018-09-08 15:27:10      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:exce   get   out   settime   import   时间   start   class   max   

package com.test.TestBoot.SingleModel;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
    public static void main(String[] args) {
 
        /**
         * Date 转 String
         */
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);       
        System.out.println(dateStr);
        
        
        /**
         *String 转  Date
         */
        String ss = "2016-10-24 21:59:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d = sdf.parse(ss);
            System.out.println(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }

 

        /**
         * 本月第一天
         */
        Calendar monthca = Calendar.getInstance();    
        monthca.add(Calendar.MONTH, 0);    
        monthca.set(Calendar.DAY_OF_MONTH,1);            
        String monthfirstDay = sdf.format(monthca.getTime());    //本月第一天
        System.out.println(monthfirstDay);

       

         /**
         * 本月最后一天
         */
        Calendar calendar = Calendar.getInstance();        
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        String monthlastday = sdf.format(calendar.getTime());    //本月最后一天
        System.out.println(monthlastday);

 

       /**
         * 昨天
         */
        Calendar startca = Calendar.getInstance();    // 得到一个Calendar的实例
        startca.setTime(new Date());                    // 设置时间为当前时间
        startca.add(Calendar.DATE, -1);                // 日期减1
        Date sDate = startca.getTime();                //前一天的时间
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");                                    
        String starttime = sd.format(sDate);                    //前一天的时间(昨天)
        System.out.println(starttime);

 

 

     /**
         * 上个月第一天
         */
        Calendar lastmonthfirst = Calendar.getInstance();
        lastmonthfirst.add(Calendar.MONTH, -1);
        lastmonthfirst.set(Calendar.DAY_OF_MONTH, 1);
        String lastmonthfirstday = sd.format(lastmonthfirst.getTime());    //上个月第一天
        System.out.println(lastmonthfirstday);
        
        /**
         * 上个月最后一天
         */
        Calendar lastmonthlast = Calendar.getInstance();
        lastmonthlast.set(Calendar.DAY_OF_MONTH, 1);
        lastmonthlast.add(Calendar.DATE, -1);
        String lastmonthlastday = sd.format(lastmonthlast.getTime());        //上个月最后一天    
        System.out.println(lastmonthlastday);

         
    }
   
}

java常见日期格式转换以及日期的获取

标签:exce   get   out   settime   import   时间   start   class   max   

原文地址:https://www.cnblogs.com/chaiming520/p/9608982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!