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);