在给定的图像中,第一个日期是当前日期以及月份和日期。 另一个日期是当前日期的第 7 天。
我尝试使用以下代码::
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd.MM.yyyy");
for (int i = 0; i < 7; i++) {
Log.i("dateTag", sdf.format(cal.getTime()));
cal.add(Calendar.DAY_OF_WEEK, 1);
}
tvDate1=(TextView) findViewById(R.id.calender_view1);
tvDate1.setText("" + DateFormat.format("dd/MM/yyyy", System.currentTimeMillis()));
tvDate2=(TextView) findViewById(R.id.calender_view2);
tvDate2.setText("here want to print 7thday");
SimpleDateFormat dateFormat= new SimpleDateFormat("EEEE dd.MM.yyyy");
Calendar currentCal = Calendar.getInstance();
String currentdate=dateFormat.format(currentCal.getTime());
currentCal.add(Calendar.DATE, 7);
String toDate=dateFormat.format(currentCal.getTime());
您需要每天的字符串,然后在 DAY_OF_WEEK 上使用开关。
检查这个 Java 类
import java.util.Calendar;
import java.util.TimeZone;
public class DateAndTimeArithmetic {
public static void main(String args[]){
//Java calendar in default timezone and default locale
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("current date: " + getDate(cal));
//adding days into Date in Java
cal.add(Calendar.DATE, 2);
System.out.println("date after 2 days : " + getDate(cal));
//subtracting days from Date in Java
cal.add(Calendar.DATE, -2);
System.out.println("date before 2 days : " + getDate(cal));
//adding moths into Date
cal.add(Calendar.MONTH, 5);
System.out.println("date after 5 months : " + getDate(cal));
//subtracting months from Date
cal.add(Calendar.MONTH, -5);
System.out.println("date before 5 months : " + getDate(cal));
//adding year into Date
cal.add(Calendar.YEAR, 5);
System.out.println("date after 5 years : " + getDate(cal));
//subtracting year from Date
cal.add(Calendar.YEAR, -5);
System.out.println("date before 5 years : " + getDate(cal));
//date after 200 days from now, takes care of how many days are in month
//for years calendar takes care of leap year as well
cal.add(Calendar.DATE, 200);
System.out.println("date after 200 days from today : " + getDate(cal));
System.out.println("current time in GMT: " + getTime(cal));
//adding hours into Date
cal.add(Calendar.HOUR_OF_DAY, 3);
System.out.println("Time after 3 hours : " + getTime(cal));
//subtracting hours from Date time
cal.add(Calendar.HOUR_OF_DAY, -3);
System.out.println("Time before 3 hours : " + getTime(cal));
//adding minutes into Date time
cal.add(Calendar.MINUTE, 3);
System.out.println("Time after 3 minutes : " + getTime(cal));
//subtracting minutes from Date time
cal.add(Calendar.HOUR_OF_DAY, -3);
System.out.println("Time before 3 minuets : " + getTime(cal));
}
/**
*
* @return current Date from Calendar in dd/MM/yyyy format
* adding 1 into month because Calendar month starts from zero
*/
public static String getDate(Calendar cal){
return "" + cal.get(Calendar.DATE) +"/" +
(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.YEAR);
}
/**
*
* @return current Date from Calendar in HH:mm:SS format
*
* adding 1 into month because Calendar month starts from zero
*/
public static String getTime(Calendar cal){
return "" + cal.get(Calendar.HOUR_OF_DAY) +":" +
(cal.get(Calendar.MINUTE)) + ":" + cal.get(Calendar.SECOND);
}
}
Output:
current date: 23/7/2012
date after 2 days : 25/7/2012
date before 2 days : 23/7/2012
date after 5 months : 23/12/2012
date before 5 months : 23/7/2012
date after 5 years : 23/7/2017
date before 5 years : 23/7/2012
date after 200 days from today : 8/2/2013
current time in GMT: 6:12:53
Time after 3 hours : 9:12:53
Time before 3 hours : 6:12:53
Time after 3 minutes : 6:15:53
Time before 3 minuets : 3:15:53
如果需要更多方法来解决您的问题,请查看此 stackoverflow 帖子 如何在 Java 中将日期增加一天?
永远不要使用有严重缺陷的遗留日期时间类,例如
Calendar
、SimpleDateFormat
、Date
等。这些已被 JSR 310 中定义的现代 java.time 类取代。
获取今天的日期。这需要一个时区,因为对于任何给定时刻,全球各地的日期都会因时区而异。日本东京是“明天”,而美国俄亥俄州托莱多则是“昨天”。
ZoneId z = ZoneId.of( "America/Edmonton" ) ; // Or ZoneId.systemDefault()
LocalDate today = LocalDate.now( z ) ;
添加 7 天。
LocalDate sevenDaysLater = today.plusDays( 7 ) ;