1、获取当前的时间 Date date=new Date();//此时date为当前的时间 2、设置时间的格式 Date date=new Date();//此时date为当前的时间 System.out.println(date);SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”);//设置当前时间的格式,为年-月-日 System.out.println(date...
java如何得到年月日。
package test;
import java.util.Calendar;
public class Test {
public static void main(String[] args) {
Calendar cal=Calendar.getInstance();//使用日历类
int year=cal.get(Calendar.YEAR);//得到年
int month=cal.get(Calendar.MONTH)+1;//得到月,因为从0开始的,所以要加1
int day=cal.get(Calendar.DAY_OF_MONTH);//得到天
int hour=cal.get(Calendar.HOUR);//得到小时
int minute=cal.get(Calendar.MINUTE);//得到分钟
int second=cal.get(Calendar.SECOND);//得到秒
System.out.println("结果:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
}
}
//程序已经写好了,已经照你说的,将结果放在变量上了,你看一下吧2010-04-21
1、获取当前的时间
Date date=new Date();//此时date为当前的时间
2、设置时间的格式
Date date=new Date();//此时date为当前的时间
System.out.println(date);
SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”);//设置当前时间的格式,为年-月-日
System.out.println(dateFormat.format(date));
SimpleDateFormat dateFormat_min=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”);//设置当前时间的格式,为年-月-日 时-分-秒
System.out.println(dateFormat_min.format(date));
扩展资料
java 获取当前微秒时间:
package com.ffcs.itm;
public class DataSecUtils {
public static void main(String[] args) {
System.out.println(System.currentTimeMillis()); // 毫秒
System.out.println(getmicTime());
System.out.println(System.currentTimeMillis()); // 毫秒
System.out.println(getmicTime());
}
/**
* @return返回微秒
*/
public static Long getmicTime() {
Long cutime = System.currentTimeMillis() * 1000; // 微秒
Long nanoTime = System.nanoTime(); // 纳秒
return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
}
}
2018-12-06
导包:
import java.util.Date;
import java.text.SimpleDateFormat;
代码:
String d=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
out.print(d);
其中"yyyy-MM-dd HH:mm:ss"可以随意组合。秒部分为ss时,显示的是秒,是SS时,显示的是毫秒。
也可以写成"yyMMddHHmmss"2015-09-25
Calendar now= Calendar.getInstance();
System.out.println("YEAR: " + now.get(Calendar.YEAR));
System.out.println("MONTH: " + now.get(Calendar.MONTH));
System.out.println("DAY_OF_MONTH: " + now.get(Calendar.DAY_OF_MONTH));
System.out.println("Hour:"+now.get(Calendar.HOUR));
System.out.println("Minute:"+now.get(Calendar.MINUTE));
System.out.println("Second:"+now.get(Calendar.SECOND));
对于获取年月日,用Calendar比较好..2010-04-20
java.util.Date date = new Date();
date.getYear();
date.getMonth();
date.getDay();
date.getHours();
date.getMinutes();
date.getSeconds();2010-04-20
上面的date.getMonth()要+1的。。。2010-04-20