获取当前系统的时间,每隔一秒,打印一次。
1 import java.util.Date; 2 3 public class TestDate { 4 public static void main(String[] args) { 5 while (true) { 6 System.out.println(new Date()); 7 8 try { 9 Thread.sleep(1000); 10 } catch (InterruptedException e) { 11 e.printStackTrace(); 12 } 13 } 14 } 15 }
获取当前系统的时间,按指定的格式排列,每隔一秒,打印一次。
1 import java.text.DateFormat; 2 import java.text.SimpleDateFormat; 3 import java.util.Date; 4 5 public class TestDate { 6 public static void main(String[] args) { 7 DateFormat df = new SimpleDateFormat("yyyy.MM.dd E a hh:mm:ss"); 8 9 while (true) { 10 System.out.println(df.format(new Date())); 11 12 try { 13 Thread.sleep(1000); 14 } catch (InterruptedException e) { 15 e.printStackTrace(); 16 } 17 } 18 } 19 }
获取今年的总天数,及已逝去的天数,算出剩余的天数。
1 import java.text.DateFormat; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.GregorianCalendar; 6 7 public class TestDate { 8 public static void main(String[] args) { 9 Calendar calendar = new GregorianCalendar(); 10 int dayOfYear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR); 11 12 DateFormat df = new SimpleDateFormat("D"); 13 int d = 0; 14 15 try { 16 d = Integer.valueOf(df.format(new Date())).intValue(); 17 } catch (NumberFormatException e) { 18 e.printStackTrace(); 19 } 20 21 System.out.println("The rest of the day is " + (dayOfYear - d) + "."); 22 } 23 }