Date 和 String 之间 转换

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {

    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        /*
         * 把java.util.Date转换为指定格式String
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        System.out.println(dateStr);//
        System.out.println(sdf1.format(date));
        //通过String.format把日期转换为String
        System.out.println(String.format("%tF %<tT", date));

        /*
         * 把String转换为Date
         */
        Date date1 = sdf.parse(dateStr);
        System.out.println(date1);
    }
}
时间: 2024-12-24 03:14:03

Date 和 String 之间 转换的相关文章

【java】Date与String之间的转换:java.text.SimpleDateFormat、public Date parse(String source) throws ParseException和public final String format(Date date)

1 package 日期日历类; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 public class TestDate { 8 public static void main(String[] args) { 9 Date date=new java.util.Date(); 10 System.out.println(date);/

HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换

1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'text/plain' }); console.info(blob); console.info(blob.slice(1, 3, 'text/plain')); 2.将TypeArray  转换成 Blob 对象 //将 TypeArray 转换成 Blob 对象 var array = new Uint16A

【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上

最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发. 先考虑的接收串口数据,那么格式是bytearray,下面需要处理成string格式来显示: #按string来显示,byarray代表接收到的数据 readstr = byarray.decode('utf-8')#这样就直接转换成str格式 #强制转换 readstr = str(byarra

Date与String之间的转换

(1) 将Date格式化为String //日期格式化 public void testFormat(){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=new Date(); String str=sdf.format(date); System.out.println(str); } (2) 将String解析为Date //和format方法相反,parse方法用于按照特定格

关于date和String互相转换的问题

其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(vo.getStarttime());if(date.getTime() < new Date().getTime()){ returnMap.put("other",

C# char 和string之间转换

har数组要转换成string可没想象的那么容易.需要使用到System.Text.StringBuilder!实例如下: char[] temp={a,b,c};System.Text.StringBuilder sb = new System.Text.StringBuilder();sb.Append(temp);string target=sb.ToString(); 反过来就比较容易了,stringValue.ToCharArrary()就可以了. from: http://blog.

Java基础——Date和String的转换

先介绍构造函数(参见API) SimpleDateFormat(String pattern)           用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat. Date转字符串: public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); String s1 =

SimpleDateFormatDemo1 Date和String格式的转换

/** *    Date重写了toString()方法,用一个字符串来描述当前Date对象所表示的时间. *    例如: Mon Feb 17 15:36:55 CST 2014 *    实际上Date的toString()方法返回的字符串虽然很清晰的描述了时间, *    但是对于非英语地区来将,该字符串不够友好,我们更希望按照特定地区表示时间的方式. *    比如我们更习惯以下的风格:    2014-02-17 15:36:55 星期一 *    java为我们提供了一个类,Sim

Java:String和Date、Timestamp之间的转换

一.String与Date(java.util.Date)互转 1.1 String -> Date Java代码   String dateStr = "2010/05/04 12:34:23"; Date date = new Date(); //注意format的格式要与日期String的格式相匹配 DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); try { date = sdf