Home  >  Article  >  Java  >  Several ways to get the current time in java

Several ways to get the current time in java

王林
王林forward
2019-12-13 11:59:312512browse

Several ways to get the current time in java

1.new Data()

SimpleDateFormat simpleFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datastring=si1.format(new Date());
//把获取的时间转换为date格式
Date insertDate=simpleFormat.parse(datastring);

Free video tutorial recommendation: java learning video

2.system gets

long current=System.currentTimeMillis();//当前时间毫秒数

3.Date class

		Calendar rightNow = Calendar.getInstance(); // 子类对象
		// 获取年
		int year = rightNow.get(Calendar.YEAR);
		// 获取月
		int month = rightNow.get(Calendar.MONTH);
		// 获取日
		int date = rightNow.get(Calendar.DATE);
		// 获取几点
		int hour = rightNow.get(Calendar.HOUR_OF_DAY);
		// 获取上午下午
		int moa = rightNow.get(Calendar.AM_PM);
		if (moa == 1) {
			System.out.println("下午");
		} else {
			System.out.println("上午");
		}
		System.out.println(year + "年" + (month + 1) + "月" + date + "日" + hour + "时");
		rightNow.add(Calendar.YEAR, 5);
		rightNow.add(Calendar.DATE, -10);
		int year1 = rightNow.get(Calendar.YEAR);
		int date1 = rightNow.get(Calendar.DATE);
		System.out.println(year1 + "年" + (month + 1) + "月" + date1 + "日" + hour + "时");

Related article tutorial recommendation:java Getting Started with Development

The above is the detailed content of Several ways to get the current time in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete