请在标记副本之前仔细阅读问题。
我想要 对应的 日期的上一个日期。 (不是昨天的日期)
e.g。如果用户点击按钮,他将被导航到另一个屏幕,并显示有关昨天的数据。
如果他再次点击该屏幕上的相同按钮,那么数据应该在前天显示....依此类推......直到我的数据库中出现数据。
所以我想获得相应日期的前一个日期。即如果我有2014年1月31日的日期(我使用格式31012014存储在数据库中)那么我应该得到日期30012014。
我知道如何获得昨天的日期,例如下面的代码
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.getDefault());
String yesterdayAsString = dateFormat.format(calendar.getTime());
它给出了与今天相比的日期,但我希望以前的日期与其他有效日期相比。
所以如何做到这一点。
答案 0 :(得分:10)
您必须使用SimpleDateFormat转换String>日期,日期之后>例如,日历;
String sDate = "31012014";
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.getDefault());
Date date = dateFormat.parse(sDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -1);
String yesterdayAsString = dateFormat.format(calendar.getTime());
答案 1 :(得分:2)
使用它,它的工作和测试代码。
private String getPreviousDate(String inputDate){
inputDate = "15-12-2015"; // for example
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format.parse(inputDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, -1);
inputDate = format.format(c.getTime());
Log.d("asd", "selected date : "+inputDate);
System.out.println(date);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
inputDate ="";
}
return inputDate;
}
答案 2 :(得分:1)
尝试这个
String prevDate;
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String todayDate=df.format(c);
Date date = null;
try {
date = df.parse(todayDate);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -1);
prevDate = df.format(calendar.getTime());
Test=(TextView)findViewById(R.id.test);
Test.setText(prevDate);
答案 3 :(得分:1)
使用现代的 java.time 类。重复减去一天以使时间倒退。
LocalDate // Represent a date-only value, without time-of-day and without time zone.
.now( // Determine the current date as perceived in the wall-clock time used by the people of a particular region (a time zone).
ZoneId.of( "Europe/Paris" ) // Use real time zone names in `Continent/Region` format, never 2-4 letter pseudo-zones such as PST, EST, IST, CEST, etc.
) // Returns a `LocalDate` object.
.minusDays( 1 ) // Move back in time by one day, for yesterday’s date. Returns another separate `LocalDate` object rather than modify the original, per Immutable Objects pattern.
.minusDays( 1 ) // Continue moving back in time another day.
.minus(
Period.ofDays( 1 ) // Define a span-of-time as any number of years-months-weeks-days.
) // Continuing to subtract yet another day.
.toString() // Generate text representing that last generated `LocalDate` date-value using standard ISO 8601 format.
解析文本输入时。
LocalDate
.parse(
"30012014" ,
DateTimeFormatter.ofPattern( "ddMMuuuu" )
)
.minusDay( 1 )
.minus(
Period.ofDays( 1 )
)
现代方法使用了几年前取代麻烦的旧日期时间类的 java.time 类。
LocalDate
LocalDate
类表示没有日期,没有time zone或offset-from-UTC的仅日期值。
时区对于确定日期至关重要。在任何给定时刻,日期都会在全球范围内变化。例如,Paris France午夜之后的几分钟是新的一天,而Montréal Québec仍然是“昨天”。
如果未指定时区,则JVM隐式应用其当前的默认时区。该默认值可能在运行时(!)期间change at any moment,因此您的结果可能会有所不同。最好将desired/expected time zone明确指定为参数。
以continent/region
的格式指定proper time zone name,例如America/Montreal
,Africa/Casablanca
或Pacific/Auckland
。切勿使用2-4个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
如果要使用JVM的当前默认时区,请提出要求并作为参数传递。如果省略,则会隐式应用JVM的当前默认值。最好明确一点,因为默认值可能会在运行时的任何时候被JVM中任何应用程序的任何线程中的任何代码更改。
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
或指定日期。您可以用数字设置月份,一月至十二月的理智编号为1-12。
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
或者更好的是,使用预定义的Month
枚举对象,每年的每个月使用一个。提示:在整个代码库中使用这些Month
对象,而不是仅使用整数,可以使您的代码更具自文档性,确保有效值并提供type-safety。
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
您的问题尚不清楚,但似乎您只是想一次一天一次地向后递增。使用LocalDate
类提供plus
和minus
方法很容易。
调用便捷方法LocalDate::minusDays
。
LocalDate yesterday = LocalDate.now( ZoneId.of( "Africa/Tunis" ) ).minusDays( 1 ) ;
要向后移动,请再次减去。
LocalDate localDatePrior = yesterday.minusDays( 1 ) ;
然后继续。
localDatePrior = localDatePrior.minusDays( 1 ) ;
您可以使用Period
类和LocalDate.minus
方法对要减去的时间段进行软编码。
Period p = Period.ofDays( 1 ) ;
LocalDate localDatePrior = yesterday.minus( p ) ;
(我正在使用格式31012014来存储在db中)
不要。
要在数据库中存储仅日期值,请在列中使用仅日期类型。在与SQL兼容的数据库中,类型为DATE
(仅日期)。
从JDBC 4.2开始,我们可以直接与数据库交换 java.time 对象。
myPreparedStatement.setObject( … , localDate ) ;
检索。
LocalDate localDate = myResultSet.getObject( … , LocalDate.class ) ;
但是要直接解决您当前的情况,您可以使用其特殊格式DDMMYYYY解析字符串。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "ddMMuuuu" ) ;
LocalDate localDate = LocalDate.parse( "30012014" , f ) ;
String output = localDate.toString() ; // Generate text in standard ISO 8601 format.
顺便说一句,而不是发明自己的日期时间格式,在交换日期时间值作为文本时,请始终使用标准的ISO 8601格式。 java.time 类在解析/生成字符串时默认情况下明智地使用这些格式。
java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date
,Calendar
和SimpleDateFormat
。
目前位于Joda-Time的maintenance mode项目建议迁移到java.time类。
要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*
类。
在哪里获取java.time类?
答案 4 :(得分:0)
你已经把一切都弄好了,除非你和#34;添加" -1天,您需要将其设置为您想要的日期(在找到上一个日期之前):
Calendar calendar = Calendar.getInstance();
calendar.set(2014, Calendar.JUNE, 9);
calendar.add(Calendar.DATE, -1);
...
答案 5 :(得分:0)
首先,作为提示,最好将日期存储为时间戳,这样您就不会依赖时间格式。
至于你的问题,只需将当前日期保存在变量中,并在点击按钮后将其发送到您的方法,然后减去额外的一天
Calendar curDate = Calendar.getInstance();
curDate.add(Calendar.DATE, -1);
然后使用你的curDate变量
答案 6 :(得分:0)
试试这个:
final Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
String yesterdayAsString = fmtOut.format(calendar.getTime());
答案 7 :(得分:0)
long calendar=Calendar.getInstance().getTimeInMillis()-1000*60*60*24;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
String date = sdf.format(calendar);