zoukankan      html  css  js  c++  java
  • 关于时间方法(date和simpledateformat)的实验

    java.text.SimpleDateFormat的用法

    利用SimpleDateFormat将String转换为格式化的日期

    参考这两篇文章得到了一些想法:

    我们的时间的输入是string格式的,要想计算什么之类的,就必须转换成时间(Date)格式,这样就能对时间进行计算比较之类的操作

    根据上面的文章修改了程序(项目ForTest)

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateTest {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
                try {
                    String time = "20120324";//string类型
                    SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMdd");//感觉就像是定义一种格式
                    // SimpleDateFormat的parse(String time)方法将String转换为Date
                    Date date = simpleDateFormat.parse(time);
                    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//又是一种新格式
                    // SimpleDateFormat的format(Date date)方法将Date转换为String
                    String formattedTime = simpleDateFormat.format(date);
                    System.out.println("---->将" + time + "解析为:" + formattedTime);
                    //实现了时间格式yyyyMMdd向格式yyyy-MM-dd的转换
                } catch (Exception e) {
                }
    
            // 将20131227085009解析为:2013-12-27 08:50:09
                try {
                    String time = "20131227085009";
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    // SimpleDateFormat的parse(String time)方法将String转换为Date
                    Date date = simpleDateFormat.parse(time);
                    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    // SimpleDateFormat的format(Date date)方法将Date转换为String
                    String formattedTime = simpleDateFormat.format(date);
                    System.out.println("---->将" + time + "解析为:" + formattedTime);
                } catch (Exception e) {
                }
                try{
                    String starttime="20150329092625";
                    String endtime="20150329103629";
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
                    Date starttime_date=sdf.parse(starttime);
                    Date endtime_date=sdf.parse(endtime);
                    long remain=starttime_date.getTime()-endtime_date.getTime();
                    System.out.println("结果为"+remain);//结果为4204000,实际的逗留事件为1小时10分钟4秒,70分钟4秒,共4204秒;
                    long min=remain/(1000*60);
                    System.out.println("逗留的分钟数"+min);//结果为70分钟,舍去了4秒
                }catch (Exception e) {
                }
    
        }
    
    }

    可以发现两个方法parse和format的区别,首先要定义格式,parse(string---Date),format(Date--string)

    另外Date类还提供了很多方法getTime方法之类的,用到再学习

  • 相关阅读:
    Qt音视频开发24-ffmpeg音视频同步
    Qt编写的项目作品34-雷达模拟仿真工具(雨田哥作品)
    Qt编写的项目作品33-斗图神器(雨田哥作品)
    Qt编写的项目作品32-定制化安装包工具(雨田哥作品)
    Qt编写的项目作品31-PDF阅读器(雨田哥作品)
    Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: ......
    com.alibaba.fastjson.JSONException: syntax error, pos 1
    chrome技巧
    pip强制重装
    javascript添加url querystring
  • 原文地址:https://www.cnblogs.com/amelie-tingting/p/6510006.html
Copyright © 2011-2022 走看看