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);
     
  • 相关阅读:
    HUD --- 3635
    leetcode380- Insert Delete GetRandom O(1)- medium
    leetcode68- Text Justification- hard
    leetcode698- Partition to K Equal Sum Subsets- medium
    leetcode671- Second Minimum Node In a Binary Tree- easy
    leetcode647- Palindromic Substrings- medium
    leetcode633- Sum of Square Numbers- easy
    leetcode605- Can Place Flowers- easy
    leetcode515- Find Largest Value in Each Tree Row- medium
    leetcode464- Can I Win- medium
  • 原文地址:https://www.cnblogs.com/binghuaZhang/p/10787511.html
Copyright © 2011-2022 走看看