Home > Java > JavaBase > body text

Several ways to get the current time in java

王林
Release: 2019-12-13 11:59:31
forward
2546 people have browsed it

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);
Copy after login

Free video tutorial recommendation: java learning video

2.system gets

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

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 + "时");
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template