Android Date类型转String 与 String转Date类型

来源:互联网 发布:kafka集群 端口 编辑:程序博客网 时间:2024/05/22 00:07

Date类型转String 与 String转Date类型,这个类型在jsp/servlet中要手动转换,而在struts2 中会自动转换SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化来看一下转换的代码:

package com.ttgbook.conver;    import java.text.DateFormat;  import java.text.SimpleDateFormat;  import java.util.Date;    public class Conver {            //把日期转为字符串      public static String ConverToString(Date date)      {          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");                    return df.format(date);      }      //把字符串转为日期      public static Date ConverToDate(String strDate) throws Exception      {          DateFormat df = new SimpleDateFormat("yyyy-MM-dd");          return df.parse(strDate);      }         }  


0 0