public static void main(String[] args) {
Calendar calendar = CalendargetInstance(LocalegetDefault());
Systemoutprintln(calendarget(CalendarYEAR));
Systemoutprintln(calendarget(CalendarMONTH)+1);
Systemoutprintln(calendarget(CalendarDATE));
}
3行代码分别用于获取当前时间的年、月、日,获取月份的时候需要+1,因为月份取的索引值,从0-11
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
public class DateTime {
public static void main(String[] args) throws ParseException {
DateTime dt = new DateTime();
Date date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");
date = dtlastDayOfMonth(date);
Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));
date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");
date = dtlastDayOfMonth2(date);
Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));
date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");
date = dtlastDayOfMonth3(date);
Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));
date = new SimpleDateFormat("yyyy-MM-dd")parse("2008-02-14");
date = dtlastDayOfMonth4(date);
Systemoutprintln (new SimpleDateFormat("yyyy-MM-dd")format(date));
}
public Date lastDayOfMonth(Date date) {
Calendar cal = CalendargetInstance();
calsetTime(date);
calset(CalendarDAY_OF_MONTH, 1);
calroll(CalendarDAY_OF_MONTH, -1);
return calgetTime();
}
public Date lastDayOfMonth2(Date date) {
Calendar cal = CalendargetInstance();
calsetTime(date);
int value = calgetActualMaximum(CalendarDAY_OF_MONTH);
calset(CalendarDAY_OF_MONTH, value);
return calgetTime();
}
public Date lastDayOfMonth3(Date date) {
Calendar cal = CalendargetInstance();
calsetTime(date);
calset(CalendarDAY_OF_MONTH, 1);
caladd(CalendarMONTH, 1);
caladd(CalendarDATE, -1);
return calgetTime();
}
public Date lastDayOfMonth4(Date date) {
Calendar cal = CalendargetInstance();
calsetTime(date);
do {
caladd(CalendarDATE, 1);
}
while (calget(CalendarDATE) != 1);
caladd(CalendarDATE, -1);
return calgetTime();
}
}
int date, month;
Date time = new Date(SystemcurrentTimeMillis());
date = timegetMonth()+1;
month = timegetDate()+1;
Systemoutprintln(month+"/"+date);
Java获得当前年月日并赋值给变量,主要是使用java 提供的工具类Calendar,Date来获取,如下代码:
package comob;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
public class DateTest {
public static void main(String[] args) throws ParseException {
Calendar now = CalendargetInstance();
Systemoutprintln("年: " + nowget(CalendarYEAR));
Systemoutprintln("月: " + (nowget(CalendarMONTH) + 1) + "");
Systemoutprintln("日: " + nowget(CalendarDAY_OF_MONTH));
Systemoutprintln("时: " + nowget(CalendarHOUR_OF_DAY));
Systemoutprintln("分: " + nowget(CalendarMINUTE));
Systemoutprintln("秒: " + nowget(CalendarSECOND));
Systemoutprintln("当前时间毫秒数:" + nowgetTimeInMillis());
Systemoutprintln(nowgetTime());
Date d = new Date();
Systemoutprintln(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdfformat(d);
Systemoutprintln("格式化后的日期:" + dateNowStr);
String str = "2012-1-13 17:26:33"; //要跟上面sdf定义的格式一样
Date today = sdfparse(str);
Systemoutprintln("字符串转成日期:" + today);
}
}
输出结果:
年: 2012
月: 1
日: 13
时: 17
分: 28
秒: 19
当前时间毫秒数:1326446899902
Fri Jan 13 17:28:19 CST 2012
Fri Jan 13 17:28:19 CST 2012
格式化后的日期:2012-01-13 17:28:19
字符串转成日期:Fri Jan 13 17:26:33 CST 2012
抽了点时间写了些代码:给你看看是不是你想要的,你是初学者把,俺给你加些注释 package yjhaction; import javatextSimpleDateFormat; //定制日期格式的类 import javautilCalendar; //日历类 import javautilDate; //当前日期和时间的Data类 import javautilScanner;//读取用户输入的类 public class DataCounter{ public int getDay(String strDate) throws Exception { int day = 0; //设定一个特定的日期格式,例如2009-01-03 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //把用户输入的字符串转化为日期和时间的格式 Date date = formatparse(strDate); //返回系统的当前状态时间 Calendar cal = CalendargetInstance(); //用给定的 date 设置此 cal calsetTime(date); //返回年月日中的天数 day = calget(calDAY_OF_YEAR); return day; } public static void main(String args[]) { try { Systemoutprintln("请输入日期,格式如:2009-03-25:"); //读取用户的输入 Scanner scan = new Scanner(Systemin); String str = scannext(); int day; day = new DataCounter()getDay(str); Systemoutprintln(str + "是" + strsubSequence(0, 4) + "年的第" + day + "天"); } catch (Exception e) { // TODO 自动生成 catch 块 eprintStackTrace(); } } }
那要看对方提供的是什么对象,如果是String类型的,那么就需要先转成Date对象,再转成Calendar对象,如果直接给的Date那么更简单,转成Calendar就可以了。
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
public class Test12{
public static void main(String args[]) throws Exception{
String d1 = "2012-04-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdfparse(d1);
Calendar c = CalendargetInstance();
csetTime(date);
Systemoutprintln(cget(CalendarMONTH));
}
}
这个月份是从0开始到11的,所以4月会输出3
String str = new javatextSimpleDateFormat("yyyy-mm-")format(new javautilDate())+"04";
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
import javautilLocale;
public class Test {
public static void main(String[] args) {
Calendar calendar = CalendargetInstance(LocaleCHINA);
calendarsetTime(new Date());// 设置当前日期
int day = calendargetMinimum(CalendarDAY_OF_MONTH);// 取得当前月的最小日期(天)
calendarset(CalendarDAY_OF_MONTH, day);// 设置天
String sDate = new SimpleDateFormat("yyyy-MM-dd")format(calendargetTime());// 格式化成字符串
Systemoutprintln(sDate);// 打印
}
}
以上就是关于java获取当前时间几天/月/年前的时间方法全部的内容,包括:java获取当前时间几天/月/年前的时间方法、java怎么算出当月和上去年这个月的值怎么算、JAVA获取当前月和日(急!!!在线等)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)