Android错误的日期
我想得到当前日期和前一个月的确切日期。例如,如果今天是2014年1月13日,我想得到今天的日期,2014年1月13日和一个月前的日期,2013年12月13日。出于某种原因,我只得到了较旧的日期,而不是今天的日期 你知道为什么会这样吗 主代码Android错误的日期,android,date,Android,Date,我想得到当前日期和前一个月的确切日期。例如,如果今天是2014年1月13日,我想得到今天的日期,2014年1月13日和一个月前的日期,2013年12月13日。出于某种原因,我只得到了较旧的日期,而不是今天的日期 你知道为什么会这样吗 主代码 //define Report Start and End Dates if(startYear == 0) { // Get due date Calendar curDate = Calendar.g
//define Report Start and End Dates
if(startYear == 0)
{
// Get due date
Calendar curDate = Calendar.getInstance();
Calendar defaultStart = curDate;
defaultStart.set(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH)-1, curDate.get(Calendar.DAY_OF_MONTH));
String curYear = ((Integer)curDate.get(Calendar.YEAR)).toString();
String curMonth = ((Integer)(curDate.get(Calendar.MONTH)+1)).toString();
String curDay = ((Integer)curDate.get(Calendar.DAY_OF_MONTH)).toString();
if(curDate.get(Calendar.MONTH)+1 < 10)
curMonth = "0"+((Integer)(curDate.get(Calendar.MONTH)+1)).toString();
String defaultYear = ((Integer)defaultStart.get(Calendar.YEAR)).toString();
String defaultMonth = ((Integer)(defaultStart.get(Calendar.MONTH)+1)).toString();
String defaultDay = ((Integer)defaultStart.get(Calendar.DAY_OF_MONTH)).toString();
if(defaultStart.get(Calendar.MONTH)+1 < 10)
defaultMonth = "0"+((Integer)(defaultStart.get(Calendar.MONTH)+1)).toString();
reportEndDate = curYear+"-"+curMonth+"-"+curDay;
reportStartDate = defaultYear+"-"+defaultMonth+"-"+defaultDay;
}
else
{
reportStartDate = startMonth+"/"+startYear;
reportEndDate = endMonth+"/"+endYear;
}
TextView startDateText = (TextView)findViewById(R.id.startDateText);
startDateText.setText(reportStartDate);
TextView endDateText = (TextView)findViewById(R.id.endDateText);
endDateText.setText(reportEndDate);
<TextView android:id="@+id/startDateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/mediumDarkGray"
android:text="XXXX-XX-XX"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/mediumDarkGray"
android:text=" to " />
<TextView android:id="@+id/endDateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/mediumDarkGray"
android:text="XXXX-XX-XX"
/>
//定义报告开始和结束日期
if(startYear==0)
{
//截止日期
Calendar curDate=Calendar.getInstance();
日历默认开始=curDate;
defaultStart.set(curDate.get(Calendar.YEAR)、curDate.get(Calendar.MONTH)-1、curDate.get(Calendar.DAY\u OF_MONTH));
字符串curYear=((整数)curDate.get(Calendar.YEAR)).toString();
字符串curMonth=((整数)(curDate.get(Calendar.MONTH)+1)).toString();
字符串curDay=((整数)curDate.get(Calendar.DAY/u/u MONTH)).toString();
if(curDate.get(日历月)+1<10)
curMonth=“0”+((整数)(curDate.get(Calendar.MONTH)+1)).toString();
字符串defaultYear=((整数)defaultStart.get(Calendar.YEAR)).toString();
字符串defaultMonth=((整数)(defaultStart.get(Calendar.MONTH)+1)).toString();
字符串defaultDay=((整数)defaultStart.get(Calendar.DAY\u OF_MONTH)).toString();
if(defaultStart.get(日历月)+1<10)
defaultMonth=“0”+((整数)(defaultStart.get(Calendar.MONTH)+1)).toString();
reportEndDate=curYear+“-”+curMonth+“-”+curDay;
reportStartDate=defaultYear+“-”+defaultMonth+“-”+defaultDay;
}
其他的
{
reportStartDate=startMonth+“/”+startYear;
reportEndDate=月末+“/”+年末;
}
TextView startDateText=(TextView)findViewById(R.id.startDateText);
setText(reportStartDate);
TextView endDateText=(TextView)findViewById(R.id.endDateText);
setText(reportEndDate);
xml布局文件
//define Report Start and End Dates
if(startYear == 0)
{
// Get due date
Calendar curDate = Calendar.getInstance();
Calendar defaultStart = curDate;
defaultStart.set(curDate.get(Calendar.YEAR), curDate.get(Calendar.MONTH)-1, curDate.get(Calendar.DAY_OF_MONTH));
String curYear = ((Integer)curDate.get(Calendar.YEAR)).toString();
String curMonth = ((Integer)(curDate.get(Calendar.MONTH)+1)).toString();
String curDay = ((Integer)curDate.get(Calendar.DAY_OF_MONTH)).toString();
if(curDate.get(Calendar.MONTH)+1 < 10)
curMonth = "0"+((Integer)(curDate.get(Calendar.MONTH)+1)).toString();
String defaultYear = ((Integer)defaultStart.get(Calendar.YEAR)).toString();
String defaultMonth = ((Integer)(defaultStart.get(Calendar.MONTH)+1)).toString();
String defaultDay = ((Integer)defaultStart.get(Calendar.DAY_OF_MONTH)).toString();
if(defaultStart.get(Calendar.MONTH)+1 < 10)
defaultMonth = "0"+((Integer)(defaultStart.get(Calendar.MONTH)+1)).toString();
reportEndDate = curYear+"-"+curMonth+"-"+curDay;
reportStartDate = defaultYear+"-"+defaultMonth+"-"+defaultDay;
}
else
{
reportStartDate = startMonth+"/"+startYear;
reportEndDate = endMonth+"/"+endYear;
}
TextView startDateText = (TextView)findViewById(R.id.startDateText);
startDateText.setText(reportStartDate);
TextView endDateText = (TextView)findViewById(R.id.endDateText);
endDateText.setText(reportEndDate);
<TextView android:id="@+id/startDateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/mediumDarkGray"
android:text="XXXX-XX-XX"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/mediumDarkGray"
android:text=" to " />
<TextView android:id="@+id/endDateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/mediumDarkGray"
android:text="XXXX-XX-XX"
/>
您将日历设置回1个月,但在开始月份和结束月份(curMonth和defaultMonth)中都添加了1 发生这种情况是因为您对curDate和defaultStart使用相同的日历实例。你需要:
Calendar defaultStart = Calendar.getInstance();
你很好。与其到处乱搞,为什么不使用内置的
add(Calendar.DAY/u/u MONTH,-1)
您还可以使用
SimpleDateFormat
来设置日期格式。如果我得到一个新实例,时间在技术上不是相差很小吗?如果出于任何原因,我的用户恰好在晚上11:59使用它,它可能有两个不同的日期吗?我想你误解了Jim的答案。当您设置defaultStart=curDate时。这里只有一个日历实例,并且defaultStart和curDate都指向同一个对象。当您对这些变量名中的任何一个进行更改时,都会影响它们所指向的对象。因此,当您使用curDate month+1时,这种变化也会发生在defaultStart上,因为curDate和defaultStart实际上只是指向同一个对象的指针。不,我理解这一部分。我想问的是,如果每次我调用Calendar.getInstance()
时,它的时间不是不同吗?因此,如果该方法恰好在午夜调用,它会有两个不同的日子吗?即使您标记为正确答案,我也会补充说,在获得第二个实例后,您可以编写逻辑来检查跨午夜事件,或者在加/减一个月之前简单地使用一个来重置另一个。比如:curDate.setTime(defaultStart.getTime())