以毫米-dd-yyyy hh:mm:ss 格式设置日期的 Java 程序
让我们以毫米-dd-yyyy hh:mm:ss 格式设置日期 −
// displaying date in mm-dd-yyyy hh:mm:ss format Format f = new SimpleDateFormat("mm-dd-yyyy hh:mm:ss"); String str = f.format(new Date()); System.out.println("Current Date in MM-dd-yyyy hh:mm:ss format = "+str);
由于上面使用了 Format 和 SimpleDateFormat 类,因此导入以下包。除此之外,我们还使用了 Date −
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date;
以下是一个示例 −
示例
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z"); System.out.println("Today's date and time = "+simpleformat.format(cal.getTime())); // displaying date in mm-dd-yyyy hh:mm:ss format Format f = new SimpleDateFormat("mm-dd-yyyy hh:mm:ss"); String str = f.format(new Date()); System.out.println("Current Date in MM-dd-yyyy hh:mm:ss format = "+str); // displaying date f = new SimpleDateFormat("dd/MMMM/yyyy"); String strDate = f.format(new Date()); System.out.println("Current Date = "+strDate); // current time f = new SimpleDateFormat("HH.mm.ss Z"); String strTime = f.format(new Date()); System.out.println("Current Time = "+strTime); } }
输出
Today's date and time = Mon, 26 Nov 2018 09:34:11 +0000 Current Date in MM-dd-yyyy hh:mm:ss format = 11-26-2018 09:34:11 Current Date = 26/November/2018 Current Time = 09.34.11 +0000
广告