zoukankan      html  css  js  c++  java
  • java 学习随笔《时间篇》

    1.获取指定时间的Date

    SimpleDateFormat accountDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    String accountDateString = "2013-05-01 00:00:00";
    
    Date accountTime = accountDateFormat.parse(accountDateString);

    2.Date转换成String

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

    3.String转换成DateTime

    String testDate = "2013-01-14 16:00:00";
    SimpleDateFormat accountDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dateTime = null; 
    try {
      dateTime = accountDateFormat.parse(testDate); 
    } catch (ParseException e) {
      e.printStackTrace();
    }

    4.Date转换为Timestamp

    Date date = new Date();  
    Timestamp timestamp = new Timestamp(date.getTime()); 

    5.处理时间

    Date d = new Date();
    DateTime now = new DateTime(d);
    DateTime todayBookTime =  new DateTime(now.get(DateTimeFieldType.year()), now.get(DateTimeFieldType.monthOfYear()), now.get(DateTimeFieldType.dayOfMonth()), 16, 0);
    DateTime tomorromBookTime = todayBookTime.plusHours(24);
    DateTime yesterdayBookTime = todayBookTime.plusHours(-24);
    
    if (now.isAfter(yesterdayBookTime) && now.isBefore(todayBookTime)) {
      bookTime = todayBookTime.toDate();
    } else if (now.isAfter(todayBookTime)) {
      bookTime = tomorromBookTime.toDate();
    }
  • 相关阅读:
    [windows] gcc编译器
    [windos] 命令
    软件版本命名规范
    [linux] vim 编辑器---更改注释文字颜色
    Call Indels/SV常用软件-搬运工
    [生物信息比对软件列表]
    [samtools] 文本查看语法,浏览SNP/INDEL位点
    [python] 之 异常对象
    [python] 之 类-运算符重载
    [R] 之 管理工作空间函数
  • 原文地址:https://www.cnblogs.com/crab-allm/p/3363718.html
Copyright © 2011-2022 走看看