本文介绍了在java中获取当前月份的第一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我正在尝试往返日期,其中 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中获取当前月份的第一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!