zoukankan      html  css  js  c++  java
  • 工具类-时间转换

     字符串转时间,插入到数据库

     

        public class DateUtils {

        public static Date strToDate(String dateStr) {
      1.   Date date=null;
      2.   String regex="\d{4}-\d{1,2}-\d{1,2}";
      3.   String regex1="\d{4}-\d{1,2}-\d{1,2} \d{1,2}";
      4.   if(dateStr!=null&&(!dateStr.equals(""))) {
      5.     if(dateStr.matches(regex)) {
      6.            dateStr=dateStr+" 00:00";
      7.    }else if(dateStr.matches(regex1)) {
      8.            dateStr=dateStr+":00";
      9.                    }else
      10.                            System.out.println(dateStr+"格式不正确");
                           }
                      DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");                   
                            try {
      1.                date=(Date) dFormat.parse(dateStr);
      2.       } catch (Exception e) {
      3.              System.out.println("打印日志信息"+e);;
      4.         }
      5.   }
      6.   return date;

    数据库时间写到前端显示需要加的标签

      <fmt:formatDate value="${activity.startTime}" pattern="yyyy-MM-dd HH:mm:ss" /> 

    日期转字符串(格式化)

    1. package com.test.dateFormat;
    2. importjava.text.SimpleDateFormat;
    3. importjava.util.Date;
    4. importorg.junit.Test;
    5. publicclassDate2String {
    6.     @Test
    7.     publicvoidtest() {
    8.         Date date = newDate();
    9.         SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");
    10.         System.out.println(sdf.format(date));
    11.         sdf = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    12.         System.out.println(sdf.format(date));
    13.         sdf = newSimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    14.         System.out.println(sdf.format(date));
    15.     }
    16. }
    运行结果
    2016-10-24
    2016-10-24 21:59:06
    20161024日 21:59:06
     
    字符串转时间
    1. packagecom.test.dateFormat;
    2. importjava.text.ParseException;
    3. importjava.text.SimpleDateFormat;
    4. importorg.junit.Test;
    5. publicclassString2Date {
    6.     @Test
    7.     publicvoidtest() throwsParseException {
    8.         String string = "2016-10-24 21:59:06";
    9.         SimpleDateFormat sdf = newSimpleDateFormat("yyyy-MM-dd");
    10.         System.out.println(sdf.parse(string));
    11.     }

    }

    运行结果
    Mon Oct 2400:00:00CST 2016
     

    StringBuffer转String

    StringBuffer s=new StringBuffer();
    s.append("1");
    s.append("2");
     
    String  s1= String.format(s.toString);
     
  • 相关阅读:
    golang 识别redis key在哪个codis的slot上
    QAT SSL加速卡安装及使用
    dereferencing pointer to incomplete type错误的解决办法
    golang topN算法
    golang日志框架zap简洁配置
    golang 读取文件
    Django学习目录
    go练习:循环与函数
    全站搜索实战应用(Lucene.Net+盘古分词)
    (原创)将Datatable数据按照Excel模板格式导出
  • 原文地址:https://www.cnblogs.com/binghuaZhang/p/10787511.html
Copyright © 2011-2022 走看看