nynw.net
当前位置:首页 >> jAvA怎样获得当前时间的前几天 >>

jAvA怎样获得当前时间的前几天

public static void main(String[] args){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); //得到前一天 Date date = calendar.getTime(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Syst...

/** * 得到几天前的时间 * * @param d * @param day * @return */ public static Date getDateBefore(Date d, int day) { Calendar now = Calendar.getInstance(); now.setTime(d); now.set(Calendar.DATE, now.get(Calendar.DATE) - day); retu...

Calendar类的add方法,第二个引数用负数就行,。

Calendar calendar = Calendar.getInstance();//今天System.out.println(calendar.getTime());//10天前calendar.add(Calendar.DATE, -10);System.out.println(calendar.getTime());Fri Oct 14 22:59:27 CST 2016 Tue Oct 04 22:59:27 CST 2016

Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, 1); System.out.println(c.getTime());

Calendar cal = Calendar.getInstance(); cal.add(Calendar.Date, 30); Date date = cal.getTime();

Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。 cal.add(Calendar.DAY_OF_MONTH, 1);//取当前日期的前一天.

//得到long类型当前时间 long l = System.currentTimeMillis(); //new日期对 Date date = new Date(l); //转换提日期输出格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM- dd HH:mm:ss");System.out.println(dateFormat.form...

package com.rxlamo.zhidao;import java.text.SimpleDateFormat;import java.util.Calendar;public class Test { public static void main(String[] args) { // 获取离当前日期最近的一个星期天 getLastSunday(); } /** * 获取离当前日期最近的...

public static void main(String[] args) {Calendar calendar = Calendar.getInstance(Locale.getDefault());System.out.println(calendar.get(Calendar.YEAR));System.out.println(calendar.get(Calendar.MONTH)+1);System.out.println(calenda...

网站首页 | 网站地图
All rights reserved Powered by www.nynw.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com