public class TodayUtil { public static String getToday(){ Calendar calendar = Calendar.getInstance(); String year = String.valueOf(calendar.get(Calendar.YEAR)); String month = String.valueOf(calendar.get(Calendar.MONTH)+1); String date = String.valueOf(calendar.get(Calendar.DATE)); String day = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)); String minute = String.valueOf(calendar.get(Calendar.MINUTE)); String second = String.valueOf(calendar.get(Calendar.SECOND)); if(calendar.get(Calendar.MONTH)+1 < 10) month = "0"+month; if(calendar.get(Calendar.DATE) < 10) date = "0"+date; return year+"-"+month+"-"+date+" "+day+":"+minute+":"+second; } public static String getTodayYmd(){ Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String result = format.format(today); return result; } //获取过去的第past天 public static String getPastDate(int past) { Calendar calendar = Calendar.getInstance(); //System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past); Date today = calendar.getTime(); //System.out.println(today); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String result = format.format(today); return result; } }
时间: 2023-06-16 20:07:37 浏览: 125
Android实现仿excel数据表格效果
这是一个Java工具类,提供了以下方法:
1. getToday():获取当前日期和时间,格式为 yyyy-MM-dd HH:mm:ss。
2. getTodayYmd():获取当前日期,格式为 yyyy-MM-dd。
3. getPastDate(int past):获取过去第 past 天的日期,格式为 yyyy-MM-dd。
这些方法都是基于当前系统时间的 Calendar 对象进行计算得出的。
阅读全文