
java simpledateformat是什么?让我们一起来了解一下吧!
其实,作为一名Java程序员,会经常在编程时候和时间打交道,比如要把某一个时间存储到数据库中去:
Datedate = newDate(); System.out.println(date);
得到的时间是这样的
SunJun 07 17:22:52CST 2020
因此,需要把时间进行格式化处理,然后再进行存储方便阅读。这个时候就会使用到SimpleDateFormat类,比如使用下面的代码来获取当前时间,并调用SimpleDateFormat 对时间进行格式化:
Date date = newDate();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String datestr = df.format(date);
System.out.println(datestr);最终输出的时间为
2020-06-07 16:45:58

simpledateformat构造方法摘要:
1. simpledateformat():以默认的模式和语言环境构造simpledateformat。
2. simpledateformat(string pattern):以自己设定的模式和语言环境构造simpledateformat。
3. simpledateformat(string pattern,dateformatsymbols):以自己设定的模式和日期符号构造simpledateformat。
java simpledateformat方法具体代码如下:
package ceshi;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Ceshi {
public static void main(String[] args) throws ParseException {
SimpleDateFormat CeshiFmt0=new SimpleDateFormat("Gyyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat CeshiFmt1=new SimpleDateFormat("yyyy/MM/dd HH:mm");
SimpleDateFormat CeshiFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat CeshiFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
SimpleDateFormat CeshiFmt4=new SimpleDateFormat("yyyy/MM/dd E");
SimpleDateFormat CeshiFmt5=new SimpleDateFormat(
"一年中的第 D 天 ,第w个星期 ,一个月中第W个星期 ,k时 z时区");
Date now=new Date();
System.out.println(CeshiFmt0.format(now));
System.out.println(CeshiFmt1.format(now));
System.out.println(CeshiFmt2.format(now));
System.out.println(CeshiFmt3.format(now));
System.out.println(CeshiFmt4.format(now));
System.out.println(CeshiFmt5.format(now));
}
}综上所述,java simpledateformat是java程序中的关于日期的一种方法。dateformat是关乎时间/日期格式子类的抽象类,它是通过与语言无关的方法去达到格式化且解析日期或者时间的目的。而simpledateformat就是dateformat的子类。
以上就是小编今天的分享了,希望可以帮助到大家。
