我正在尝试往返日期,其中 ToDate
将具有上一个日期,而 FromDate
将具有当月的第一个日期.对于 1 月,它将是 1/1/2013
等等.如何正确获取当月的第一个日期?我做不到.
I am trying to get to and from date where ToDate
will have previous date and FromDate
will have first date of the current month. For January it would be 1/1/2013
and so on. How to get the first date of the current month correctly? I am not able to do it.
today.add(Calendar.DAY_OF_MONTH, -1);
java.util.Date previousDay=today.getTime();
ToDate = sdfFile1.format(newjava.sql.Date(previousDay.getTime()));
today.add(Calendar.DATE, 1);
java.util.Date nextDay=today.getTime();
FromDate = sdfFile1.format(new java.sql.Date(nextDay.getTime()));
试试
Calendar c = Calendar.getInstance(); // this takes current date
c.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(c.getTime()); // this returns java.util.Date
已更新(自 Java 8):
import java.time.LocalDate;
LocalDate todaydate = LocalDate.now();
System.out.println("Months first date in yyyy-mm-dd: " +todaydate.withDayOfMonth(1));
这篇关于在java中获取当前月份的第一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!