java formats the date:
import java.text.SimpleDateFormat; import java.util.Date; public class test{ public static void main(String args[]) { Date newTime = new Date(); //设置时间格式 SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d h:m:s a E"); SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd hh:mm:ss a E"); SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss a E"); SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyy-MMMM-dddd hhhh:mmmm:ssss a E"); //获取的时间,是本机的时间 String formatDate1 = sdf1.format(newTime); String formatDate2 = sdf2.format(newTime); String formatDate3 = sdf3.format(newTime); String formatDate4 = sdf4.format(newTime); System.out.println(formatDate1); System.out.println(formatDate2); System.out.println(formatDate3); System.out.println(formatDate4); } }
The output results are as follows:
Use the SimpleDateFormat class in java The constructor SimpleDateFormat(String str) constructs the format of the formatted date.
Formats the specified date object into a string of the specified format through the format(Date date) method.
More java For knowledge, please pay attention to java basic tutorial.
The above is the detailed content of How to format date in java. For more information, please follow other related articles on the PHP Chinese website!