java时间的格式化
Ⅰ java时间格式化
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.*;
classSimpleDateFormat01
{
publicstaticvoidmain(String[]args)throwsParseException
{
Stringstr="FriOct2715:38:07CST2017";
SimpleDateFormatsdf1=newSimpleDateFormat("EEEMMMddHH:mm:sszzzyyyy",Locale.US);
SimpleDateFormatsdf2=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Datedate=sdf1.parse(str);//提取格式中的日期
StringnewStr=sdf2.format(date);//改变格式
System.out.println("转换之后的日期:"+newStr);
}
}
Ⅱ JAVA 中获取时间怎么格式化
时间格式化输出主要有两种方式,代码如下:
//使用Calendar
Calendar now = Calendar.getInstance();
System.out.println("年:" + now.get(Calendar.YEAR));
System.out.println("月:" + (now.get(Calendar.MONTH) + 1));
System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分:" + now.get(Calendar.MINUTE));
ystem.out.println("秒:" + now.get(Calendar.SECOND));
//使用Date
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间:" + sdf.format(d));
(2)java时间的格式化扩展阅读
JAVA中获取当前系统时间。
import java.util.Date;
import java.text.SimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
}
}
参考资料来源:网络:Java
Ⅲ Java格式化字符串日期
可以通过java类中SimpleDateFormat方法类来实现时间的格式化
Date date= new Date();//创建一个时间对象,获取到当前的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间显示专格式,根属据实际需要修改即可
String str = sdf.format(date);//将当前时间格式化为需要的类型
System.out.println(str);//输出结果
结果为:2015-11-05 13:45:54(实时)。
Ⅳ java将时间格式化(急)
下面是我以前写过的算法,是不是你想要的.
DateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
Date dDate = format.parse("200911120000");
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String reTime = format2.format(dDate);
System.out.println(reTime);
Ⅳ java中的日期格式化怎么做的
String fmt = "";譬如要format为yyyyMM-dd
fmt = "yyyyMM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
Date date = ....;
String dateStr = sdf.format(date);
字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800
Ⅵ java中怎么格式化日期
你可以用String类的format方法,例如: System.out.println(String.format("%ty年%<tm月%<td日",date));下面是一个完整的例子。
publicclassFormatDateTest
{
publicstaticvoidmain(String[]args)
{
Datedate=newDate(System.currentTimeMillis());
System.out.println(String.format("%ty年%<tm月%<td日",date));
System.out.println(String.format("%tY年%<tm月%<td日",date));
System.out.println(String.format("%tY年%<tm月%<td日%<tH时%<tM分%<tS秒",date));
}
}
%ty是格式化年,%tm是格式化年,%td是格式化天,%tH格式化发时,%tM格式化分,%tS格式化秒。另外%tY是把年格式化为四位形式,如1999,而不是99。%tI是把时格式化为12小时制。格式化字符串中的<是表示格式化同一个日期,当然你也可以这么写:System.out.println(String.format("%ty年%tm月%td日",date,date,date));
Ⅶ Java日期显示格式化
把data类型改成String或者,在这bookDto的getReleaseDate方法里返回一个字符串
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
String strDate = sdf.format(date); //这样就得到格式化后的字符串
Ⅷ java中时间格式化输出
使用SimpleDateFormat即可,代码如下
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassDateUtil{
publicstaticStringformatDate(Datedate)throwsParseException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
returnsdf.format(date);
}
publicstaticDateparse(StringstrDate)throwsParseException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
returnsdf.parse(strDate);
}
}
Ⅸ java日期格式化
Date d=new Date();
SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日 E");
System.out.println(sf.format(d));
Ⅹ 如何将JAVA DATE类型的日期 转换成指定格式类型的 (如:YYYY-MM-DD) 的 DATE类型数据
Date类型并没有格式,只有转换成String格式的时候让格式化显示。
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());
Calendar calendar = Calendar.getInstance();
int year = Integer.parseInt(datetime.substring(0,4));
int month = Integer.parseInt(datetime.substring(5,7));
int date = Integer.parseInt(datetime.substring(8,10));
int hour = Integer.parseInt(datetime.substring(11,13));
int minute = Integer.parseInt(datetime.substring(14,16));
//int second = Integer.parseInt(datetime.substring(17,19));
if(calendar.get(Calendar.YEAR)>year){
int y = calendar.get(Calendar.YEAR)-year;
(10)java时间的格式化扩展阅读:
Date类可以在java.util包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),创建一个表示创建时刻的对象。getTime()方法返回Date对象的long值。
import java.util.*;
public class Now {
public static void main(String[] args) {
Date now = new Date();
long nowLong = now.getTime();
System.out.println("Value is " + nowLong);