zoukankan      html  css  js  c++  java
  • 爪哇国新游记之三十一----日期时间与字符串间的转化

    1.由日期时间转化成字符串

    Date date = new Date();
    Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateString=formatter.format(date);

    上述代码使用的是SimpleDateFormat的format函数

    2.由字符串转化成日期时间

    String dateStr1="20141216";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date date1 = sdf.parse(dateStr1);

    上述代码使用的是SimpleDateFormat的parse函数。

    3.得到几天前的日期

        public static String getDateBefore(Date d, int day) {
            Calendar now = Calendar.getInstance();
            now.setTime(d);
            now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
            
            Format formatter = new SimpleDateFormat("yyyy-MM-dd");
            return formatter.format(now.getTime());
        }

    4.得到一个月的第一天,这个比较简单

    String endDate="2014-12-22";
    String[] arr=endDate.split("-");
    String startDate=arr[0]+"-"+arr[1]+"-"+"01";

    5.得到一周的第一天

    String newEndDate="2014-12-22";
    
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    Date date=fmt.parse(newEndDate);
    Calendar c=Calendar.getInstance();
    c.setTime(date);
    int weekday=c.get(Calendar.DAY_OF_WEEK);
                
    String startDate=DateTimeUtil.getDateBefore(date, weekday-1);
  • 相关阅读:
    第四周学习进度总结
    SOA面向服务的架构
    MVC架构模式
    大型网站技术架构阅读笔记01
    Python爬虫出错
    修改安卓的gradle地址后出现cannot resolve symbol ......错误
    一线架构师阅读笔记03
    周进度报告(十)
    周进度报告(九)
    一线架构师阅读笔记02
  • 原文地址:https://www.cnblogs.com/heyang78/p/4166264.html
Copyright © 2011-2022 走看看