1、建議控制台程序
使用 IntelliJ IDEA 創建 MAVEN項目, 不選別的選項,則為控制台程序。 (其它方式創建的控制台程序可能編譯不過)
2、源碼如下:
1 import java.time.LocalDateTime; 2 import java.time.format.DateTimeFormatter; 3 import java.time.format.FormatStyle; 4 import java.util.Locale; 5 6 public class Main { 7 public static void main(String[] args) 8 { 9 System.out.println("Hello World"); 10 //Locale local = new Locale("zh","CN"); //Locale.CHINA 11 //Locale.setDefault(Locale.US); 12 Locale.setDefault(Locale.CHINA); 13 DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); 14 LocalDateTime now = LocalDateTime.now(); 15 String date = df.format(now); 16 System.out.println(date); 17 } 18 }
3、上面使用了JAVA8的API, 需要在FILE菜單 Project Structure中,調整 Language level

4、 其它的一些發現:
1 //服務端請不要直接使用下面的代碼,多線程環境下存在沖突問題 2 //美國日期時間格式化 3 Locale.setDefault(Locale.US); 4 SimpleDateFormat sdf = new SimpleDateFormat(); 5 System.out.println("美國時間: "+sdf.format(new Date())); 6 7 // 中國日期時間格式化 8 Locale.setDefault(Locale.CHINA); 9 sdf = new SimpleDateFormat(); 10 System.out.println("中國時間: "+sdf.format(new Date()));
