JAVA— 時間格式與動態時間獲取應用總結
阿新 • • 發佈:2018-12-25
目錄
java實現獲取當前年、月、日 、小時 、分鐘、 秒、 毫秒
Java獲取日期常用格式
第一個直接上程式碼:
package org.time; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class Time { public void getTimeByDate() { Date date = new Date(); // 日期格式,精確到日 DateFormat df1 = DateFormat.getDateInstance(); System.out.println(df1.format(date)); // 精確到時分秒 DateFormat df2 = DateFormat.getDateTimeInstance(); System.out.println(df2.format(date)); // 只顯示時分秒 DateFormat df3 = DateFormat.getTimeInstance(); System.out.println(df3.format(date)); // 顯示日期,周,上下午,精確到秒的時間 DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); System.out.println(df4.format(date)); // 顯示日期,上下午,時間(精確到秒) DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); System.out.println(df5.format(date)); // 顯示日期,上下午,時間(精確到分) DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); System.out.println(df6.format(date)); // 顯示日期,時間(精確到分) DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); System.out.println(df7.format(date)); } public void getTimeByCalendar() { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); // 獲取年份 int month = cal.get(Calendar.MONTH);// 獲取月份 int day = cal.get(Calendar.DATE);// 獲取日 int hour = cal.get(Calendar.HOUR);// 獲取小時 int minute = cal.get(Calendar.MINUTE);// 獲取分鐘 int second = cal.get(Calendar.SECOND);// 獲取秒 int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK)-1;// 獲取一週的第幾天 System.out.println("現在的時間是:公元" + year + "年" + month + "月" + day + "日 " + hour + "時" + minute + "分" + second + "秒 星期" + WeekOfYear); } }
測試:
package org.test;
import org.time.Time;
public class TimeTest {
public static void main(String[] args) {
Time time = new Time();
time.getTimeByDate();
System.out.println("--------------------------------------------");
time.getTimeByCalendar();
}
}
執行結果:
格式化輸出日期時間:(2018-12-25 10:12:37)
Date date =new Date();
SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time=dFormat.format(date);
System.out.println(time);
計算某一月份的最大天數:
Calendar time=Calendar.getInstance(); time.clear(); time.set(Calendar.YEAR,2018); //year 為 int 2018為第二個引數year time.set(Calendar.MONTH,12-1);//注意,Calendar物件預設一月為0 ,12為引數月份 int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天數 System.out.println(day);
計算某年的一天是一年中的第幾周:
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.MONTH, 12-1); //注意:預設一月為0
cal.set(Calendar.DAY_OF_MONTH, 25-1); //預設星期日為一週的第一天
int weekno=cal.get(Calendar.WEEK_OF_YEAR);
System.out.println(weekno);
計算一年中的第幾星期是幾號:
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.WEEK_OF_YEAR, 51);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//某一週的星期一時間
System.out.println(df.format(cal.getTime()));
以上學習總結參考:http://www.blogjava.net/xiaoyi/articles/295044.html
java實現獲取當前年、月、日 、小時 、分鐘、 秒、 毫秒
package org.time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtil01 {
public static String FORMAT_SHORT="yyyy-MM-dd";
public static String FORMAT_LONG="yyyy-MM-dd HH:mm:ss";
public static String FORMAT_FULL="yyyy-MM-dd HH:mm:ss.S";
public static String FORMAT_SHORT_CN="yyyy年MM月dd日 HH時mm分ss秒";
public static String FORMAT_LONG_CN="yyyy年MM月dd日 HH時mm分ss秒SSS毫秒";
/*
* 獲取當前時間 年月日時分秒毫秒
*/
public static String getTimeString(){
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/*
* 獲取年份
*/
public static String getYear(Date date){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy");
Calendar calendar =Calendar.getInstance();
return sdf.format(calendar.getTime());
}
/*
* 獲取月份
*/
public static int getMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH)+1;
}
/*
* 獲取日分
*/
public static int getDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
/*
* 獲取小時
*/
public static int getHour(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/*
* 獲取分鐘
*/
public static int getMinute(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
}
/*
* 獲取秒
*/
public static int getSecond(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
}
/*
* 獲取毫秒
*/
public static long getMillis(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
public static void main(String[] args) {
System.out.println(getTimeString());
System.out.println("返回日期年份:"+getYear(new Date()));
System.out.println("返回月份:"+getMonth(new Date()));
System.out.println("返回當天日份:"+getDay(new Date()));
System.out.println("返回當天小時:"+getHour(new Date()));
System.out.println("返回當天分:"+getMinute(new Date()));
System.out.println("返回當天秒:"+getSecond(new Date()));
System.out.println("返回當天毫秒:"+getMillis(new Date()));
}
}
參考:http://yuncode.net/code/c_5194eee4e69e95
https://blog.csdn.net/zs20082012/article/details/56841368/
Java執行緒動態時間:
package org.time;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeTest extends JFrame implements Runnable {
private JFrame frame;
private JPanel timePanel;
private JLabel timeLabel;
private JLabel displayArea;
private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
private int ONE_SECOND = 1000;
public TimeTest() {
timePanel = new JPanel();
timeLabel = new JLabel("CurrentTime: ");
displayArea = new JLabel();
timePanel.add(timeLabel);
timePanel.add(displayArea);
this.add(timePanel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(new Dimension(200, 70));
this.setLocationRelativeTo(null);
}
public void run() {
while (true) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
displayArea.setText(dateFormatter.format(Calendar.getInstance().getTime()));
try {
Thread.sleep(ONE_SECOND);
} catch (Exception e) {
displayArea.setText("Error!!!");
}
}
}
public static void main(String arg[]) {
TimeTest df2 = new TimeTest();
df2.setVisible(true);
Thread thread1 = new Thread(df2);
thread1.start();
}
}
參考學習:https://www.jb51.net/article/53597.htm