目录
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();
}
}