各位老铁们好,相信很多人对j *** a格式化时间都不是特别的了解,因此呢,今天就来为大家分享下关于j *** a格式化时间以及j *** a字符串格式化 *** 的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!
本文目录
一、请问J *** A中获取 *** 当前时间该怎么写
*** 一:用j *** a.util.Date类来实现,并结合j *** a.text.DateFor *** t类来实现时间的格式化,看下面代码:
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.j *** a
public static void *** in(String[] args){
Calendar cal= Calendar.getInstance();
DateFor *** t d1= DateFor *** t.getDateInstance();//默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
DateFor *** t d2= DateFor *** t.getDateTimeInstance();
DateFor *** t d3= DateFor *** t.getTimeInstance();
DateFor *** t d4= DateFor *** t.getInstance();//使用SHORT风格显示日期和时间
DateFor *** t d5= DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL);//显示日期,周,时间(精确到秒)
DateFor *** t d6= DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG);//显示日期。时间(精确到秒)
DateFor *** t d7= DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT);//显示日期,时间(精确到分)
DateFor *** t d8= DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM);//显示日期,时间(精确到分)
String str8= d8.for *** t(now);//与SHORT风格相比,这种方式更好用
System.out.println("用Date方式显示时间:"+ now);//此 *** 显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFor *** t.getDateInstance()格式化时间后为:"+ str1);
System.out.println("用DateFor *** t.getDateTimeInstance()格式化时间后为:"+ str2);
System.out.println("用DateFor *** t.getTimeInstance()格式化时间后为:"+ str3);
System.out.println("用DateFor *** t.getInstance()格式化时间后为:"+ str4);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL)格式化时间后为:"+ str5);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG)格式化时间后为:"+ str6);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT)格式化时间后为:"+ str7);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM)格式化时间后为:"+ str8);
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFor *** t.getDateInstance()格式化时间后为:2008-6-16
用DateFor *** t.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFor *** t.getTimeInstance()格式化时间后为:20:54:53
用DateFor *** t.getInstance()格式化时间后为:08-6-16下午8:54
用DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL)格式化时间后为
:2008年6月16日星期一下午08时54分53秒 CST
用DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG)格式化时间后为
:2008年6月16日下午08时54分53秒
用DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT)格式化时间后
用DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM)格式化时间
*** 二:用j *** a.util.Calendar类来实现,看下面:
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public static void *** in(String[] args){
Calendar ca= Calendar.getInstance();
int year= ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH);//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int ho *** =ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear= ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式显示时间:"+ ca.getTime());
System.out.println("用Calendar获得日期是:"+ year+"年"+ month+"月"+ day+"日");
System.out.println("用Calendar获得时间是:"+ ho *** +"时"+ minute+"分"+ second+"秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒
总结:中的来说, *** 二是最方便的, *** 一显得分笨拙,不过看个人喜欢了。
二、j *** a怎么格式化输入日期
1、是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期->文本)、解析(文本->日期)和规范化。
2、使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过 DateFor *** t中的 getTimeInstance、getDateInstance或 getDateTimeInstance来创建日期-时间格式器。每一个这样的类 *** 都能够返回一个以默认格式模式初始化的日期/时间格式器。可以根据需要使用 applyPattern *** 来修改格式模式。
3、日期和时间格式由日期和时间模式字符串指定。在日期和时间模式字符串中,未加引号的字母'A'到'Z'和'a'到'z'被解释为模式字母,用来表示日期或时间字符串元素。文本可以使用单引号(')引起来,以免进行解释。"''"表示单引号。所有其他字符均不解释;只是在格式化时将它们简单 *** 到输出字符串,或者在解析时与输入字符串进行匹配。
4、定义了以下模式字母(所有其他字符'A'到'Z'和'a'到'z'都被保留):
三、j *** a怎么格式化时间
j *** a如何获取当前时间以及格式化需要用到两个类,如下图:
1.获取当前时间,并格式化为(年-月-日时:分:秒)。
SimpleDateFor *** t df= new SimpleDateFor *** t("yyyy-MM-dd HH:mm:ss");
System.out.println(df.for *** t(t));
2.将j *** a.util.Date转换为j *** a.sql.Date格式。
j *** a.sql.Date sqld= new j *** a.sql.Date(t.getTime());
j *** a.sql.Time sqlt= new j *** a.sql.Time(t.getTime());
j *** a.sql.Timestamp sqlts= new j *** a.sql.Timestamp(t.getTime());
J *** a是一种广泛使用的计算机编程语言,拥有跨平台、面向对象、泛型编程的特 *** ,广泛应用于企业级Web应用开发和移动应用开发。
J *** a编程语言的风格十分接近C++语言。继承了C++语言面向对象技术的核心,舍弃了容易引起错误的指针,以引用取代;移除了C++中的运算符重载和多重继承特 *** ,用接口取代;增加垃圾回收器功能。
J *** a编程语言是个简单、面向对象、分布式、解释 *** 、健壮、安全与 *** 无关、可移植、高 *** 能、多线程和动态的语言。
四、j *** a 日期格式化
你真会开玩笑,日期型就是日期型的,没有几种格式可以转换(比如格林威治时间,距历元所经过的毫秒,也有用long型来表示的,都是计算机中的一种表示而已),根本无法转换成你要的这种格式,真的要格式不过是把日期打印出来的时候的格式而已,也就是String的格式而已!
而且我感觉没有比较关注日期类型的具体类型,你要String的类型就好了:
import j *** a.text.SimpleDateFor *** t;
public static void *** in(String []aa){
SimpleDateFor *** t datefor *** t1=new SimpleDateFor *** t("yyyy-MM-dd HH:mm:ss E");
String a1=datefor *** t1.for *** t(new Date());
System.out.println("时间2:"+a1);
System.out.println(new Date().getYear()+1900);
SimpleDateFor *** t datefor *** t2=new SimpleDateFor *** t("yyyy年MM月dd日 HH时mm分ss秒 E");
String a2=datefor *** t2.for *** t(new Date());
System.out.println("时间2:"+a2);
五、在j *** a中如何把字符串转换成时间格式
1、public static void *** in(String[] args) throws Exception{
2、Date date= new SimpleDateFor *** t("yyyy-MM-dd").parse("2005-06-09");
3、String now= new SimpleDateFor *** t("yyyy年MM月dd日").for *** t(date);
4、public static void *** in(String[] args){
5、// TODO Auto-generated method stub
6、SimpleDateFor *** t df= new SimpleDateFor *** t("yyyyMMddhhmmss");
7、String dateString="20071128175545";
8、Date date= df.parse(dateString);
9、System.out.println(df.for *** t(date));
10、System.out.println(ex.getMessage());
11、J *** a指定了一些可访问的修饰符如:private、protected、public,任何 *** 或变量都可以声明为public,J *** a可以从该类之外的地方访问。因为 *** in *** 是公共的,JVM就可以轻松的访问执行它。
12、在 *** in里面args[]就是两个长度的数组:value1存在args[0]中,value2存在args[1]中。
13、args[0]存储的是命令,其后才是参数。
14、String args[ ]或者String[ ] args表示给主 *** 传一个字符串数组。
15、而args是一个字符串数组的变量名,不是关键字,是arguments的缩写,只是一个默认名,一般都习惯 *** 照写。
16、public static void *** in(String args[ ]){
17、System.out.println("This is a *** J *** a program.");
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!