java获取昨天今天明天的日期
Posted 图图小淘气_real
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java获取昨天今天明天的日期相关的知识,希望对你有一定的参考价值。
我刚写完这个代码,就被开除了........
代码双手奉上以备后用:
package mydemo; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class ThreeDay { public static void main(String[] args) { getToday(); getYesterday(); getTomorrow(); } /** * 获取今天的日期 */ public static void getToday() { //SimpleDateFormat smdate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String date = smdate.format(new Date()); System.out.println(date); } /** * 获取昨天的日期 */ public static void getYesterday() { SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); calendar.add(calendar.DATE, -1); String date = smdate.format(calendar.getTime()); System.out.println(date); } /** * 获取明天的日期 */ public static void getTomorrow() { SimpleDateFormat smdate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Calendar calendar = new GregorianCalendar(); calendar.setTime(new Date()); calendar.add(calendar.DATE, 1); String date = smdate.format(calendar.getTime()); System.out.println(date); } }
结果:
补充:
Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH)+1; int day = calendar.get(Calendar.DATE); System.out.println(year); System.out.println(month); System.out.println(day);
package util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.List; public class Time { /** * 获取指定月份 * * @param months * @return */ public static List<String> getMonthsBetween(int months) { List<String> listMonth = new ArrayList<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar c = Calendar.getInstance(); for (int i = 1; i <= months; i++) { c.setTime(new Date()); c.add(Calendar.MONTH, -i); Date m = c.getTime(); listMonth.add(sdf.format(m)); } Collections.reverse(listMonth); return listMonth; } /** * 获取前几天的日期 * * @param days * @return */ public static List<String> getDaysBetwwen(int days) { Calendar calendar = Calendar.getInstance(); List<String> listDays = new ArrayList<>(); // 1天 Long oneDay = 1000 * 60 * 60 * 24l; for (int i = 0; i < days; i++) { Date d = new Date(calendar.getTimeInMillis() - oneDay * i); DateFormat df = new SimpleDateFormat("MM月dd日"); // System.out.println(df.format(d)); listDays.add(df.format(d)); } Collections.reverse(listDays); // 倒序排列 return listDays; } public static String getLast12Months(int i) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar c = Calendar.getInstance(); c.setTime(new Date()); c.add(Calendar.MONTH, -i); Date m = c.getTime(); return sdf.format(m); } public static void main(String[] args) { List<String> daysBetwwen = getDaysBetwwen(7); System.out.println(daysBetwwen.toString()); List<String> monthsBetween = getMonthsBetween(7); // Collections.sort(monthsBetween); // 顺序排列 // Collections.shuffle(monthsBetween); // 混乱的意思 // Collections.binarySearch(monthsBetween, " a5 "); // Collections.reverse(monthsBetween);// 倒序排列 System.out.println(monthsBetween.toString()); } }
以上是关于java获取昨天今天明天的日期的主要内容,如果未能解决你的问题,请参考以下文章