我有以下疑問如何在Java中創建格式日期。如何正確創建具有特定格式的日期?
在Java應用程序,我必須創建一個日期(值必須是當前日期)這樣的格式:2015年5月26日(YYYY-MM-DD)
所以我知道我可以獲取當前的日期只是建立一個新的java.util.Date
對象,這種方式:
Date dataDocumento = new Date();
,但我怎麼可以指定我的日期格式?
TNX
我有以下疑問如何在Java中創建格式日期。如何正確創建具有特定格式的日期?
在Java應用程序,我必須創建一個日期(值必須是當前日期)這樣的格式:2015年5月26日(YYYY-MM-DD)
所以我知道我可以獲取當前的日期只是建立一個新的java.util.Date
對象,這種方式:
Date dataDocumento = new Date();
,但我怎麼可以指定我的日期格式?
TNX
嘗試這樣的:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date c= sdf.parse("2015-05-26");
String date=sdf.format(c);
System.out.println(date);
要格式化的YYYY-MM-DD格式的當前日期,你可以嘗試這樣的
Date date = new Date();
String str = new SimpleDateFormat("yyyy-MM-dd").format(date);
好的,但問題是我必須創建一個新的當前日期,不要使用2015-05-26你的工作 – AndreaNobili
@AndreaNobili: - 更新我的答案!請立即檢查! –
java.util.Date
對象本身沒有格式。它們只是時間戳值,就像int
只是一個數字,沒有任何固有的格式。所以,不存在「Date
對象格式爲yyyy-MM-dd
」。
格式在您將Date
轉換爲String
時確定。您可以使用SimpleDateFormat
以特定格式將Date
轉換爲String
。例如:
Date date = new Date();
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String text = fmt.format(date);
System.out.println(text);
你必須使用SimpleDateFormat:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
注意這MM
代表一個月,而不是mm
。
你格式化你的日期和解析它作爲一個Date
對象是這樣的:
Date dt = sf.parse(sf.format(new Date()));
使用format(new Date())
您可以格式化new Date()
。
你說「你這樣格式化你的日期」,然後你展示如何*解析*日期,這與格式相反。 – Jesper
是@Jesper你是對的,我正在編輯它。 –
嘗試以下操作:
String currentDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
代碼
進口java.text.SimpleDateFormat的; import java.util。日期;
/**
*
* Java program to show how to format date in Java using SimpleDateFormat
* Examples. Java allows to include date, time and timezone information
* while formatting dates in Java.
*
* @author http://java67.blogspot.com
*/
public class DateFormatExample {
public static void main(String args[]) {
// This is how to get today's date in Java
Date today = new Date();
//If you print Date, you will get un formatted output
System.out.println("Today is : " + today);
//formatting date in Java using SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
String date = DATE_FORMAT.format(today);
System.out.println("Today in dd-MM-yyyy format : " + date);
//Another Example of formatting Date in Java using SimpleDateFormat
DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
date = DATE_FORMAT.format(today);
System.out.println("Today in dd/MM/yy pattern : " + date);
//formatting Date with time information
DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS");
date = DATE_FORMAT.format(today);
System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);
//SimpleDateFormat example - Date with timezone information
DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z");
date = DATE_FORMAT.format(today);
System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date);
}
}
輸出
Today is : Tue May 26 16:11:27 IST 2015
Today in dd-MM-yyyy format : 26-05-2015
Today in dd/MM/yy pattern : 26/05/15
Today in dd-MM-yy:HH:mm:SS : 26-05-15:16:11:316
Today in dd-MM-yy:HH:mm:SSZ : 26-05-15:16:11:316 +0530
使用以下各項
String currentDate = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
此外,您可以使用任何的formet給定的,如果須─
new SimpleDateFormat("dd/MM/yyyy").format(new Date());
new SimpleDateFormat("dd-MM-yy:HH:mm:SS").format(new Date());
new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z").format(new Date());
試試這個代碼:
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); // you can change the format as you like
Date date = new Date();
System.out.println(dateFormat.format(date));
}
}
您可以使用簡單的日期格式類這樣:
import java.text.SimpleDateFormat;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date dataDocumento = new Date();
sdf.format(dataDocumento);
請參閱以下內容:https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html –
Date沒有格式的概念,它只是自Unix紀元以來的毫秒數的容器。如果要以特定格式顯示日期值,請使用DateFormat – MadProgrammer
下面有8個過時的答案。 2017年的正確和當前答案是'LocalDate.now(ZoneId.systemDefault()).toString()'(當問題在2015年被問到時,它甚至是一個有效答案)。您可能會根據您的需求提供不同的時區。 –