简体   繁体   English

在Java中经过一定时间后获取日期

[英]Getting dates after certain period of time in Java

Basically I have a date stored as text in this format: 16/09/2014 in SQLite Browser. 基本上,我有一个日期以文本格式存储为日期: 16/09/2014在SQLite浏览器中。 I wonder is there any way to get the date after one day, one week, one month and one year of each records in the database using Java. 我想知道有没有办法使用Java获取数据库中每个记录的一天,一周,一个月和一年之后的日期。

I retrieved and display the date retrieved from database in a listview: 我检索并在列表视图中显示从数据库检索的日期:

viewHolder.txt_ddate.setText("Next Payment On: "
                + _recurlist.get(position).getRecurringStartDate().trim());

So I was thinking to use Java technique to get the dates I mentioned above. 所以我当时在考虑使用Java技术获取我上面提到的日期。 I have researched on this and found Documentation but I not sure how to implement it into my problem. 我对此进行了研究,找到了文档,但不确定如何将其实施到我的问题中。

Any guides? 有指导吗? Thanks in advance. 提前致谢。

Use a Calendar object like in your example, which provides the add method. 像您的示例一样使用Calendar对象,该对象提供add方法。

String dateAsString = "16/09/2014";
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(formatter.parse(dateAsString));

c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println("After one day: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.DAY_OF_MONTH, -1);

c.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("After one week: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.WEEK_OF_YEAR, -1);

c.add(Calendar.MONTH, 1);
System.out.println("After one month: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.MONTH, -1);

c.add(Calendar.YEAR, 1);
System.out.println("After one year: " + formatter.format(c.getTimeInMillis()));
c.add(Calendar.YEAR, -1);

Output: 输出:

After one day: 17/09/2014
After one week: 23/09/2014
After one month: 16/10/2014
After one year: 16/09/2015

With Joda-time : 乔达时间

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("16/09/2014", formatter);

System.out.println(date.toString(formatter));
System.out.println(date.plusDays(1).toString(formatter));
System.out.println(date.plusWeeks(1).toString(formatter));
System.out.println(date.plusMonths(1).toString(formatter));
System.out.println(date.plusYears(1).toString(formatter));

Output: 输出:

16/09/2014
17/09/2014
23/09/2014
16/10/2014
16/09/2015

Use Calendar api of Java/Android as follow: 按照以下方式使用Java / Android的Calendar api:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Date date;

try {
    date = sdf.parse(dateStr);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, 1); //add one day to your date
    cal.add(Calendar.MONTH, 1); //add 1 month to your date 
    cal.add(Calendar.YEAR, 1); //add 1 year to current date
    System.out.println(sdf.format(cal.getTimeInMillis()));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Here is the example: 这是示例:

String strDate = "16/09/2014";
int noOfDays = 1;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, noOfDays);

tl;dr tl; dr

LocalDate.parse( 
    "16/09/2014" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
)
.plusDays( 1 )
.format( DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) )

Details 细节

Tip: Use date-time data types for date-time values. 提示:将日期时间数据类型用于日期时间值。 You should be using a date-oriented type to define your column in your database to store a date value rather than as text. 您应该使用面向日期的类型在数据库中定义列以存储日期值而不是文本。

Tip # 2: When you do serialize a date value to text, use the standard ISO 8601 formats. 提示2:将日期值序列化为文本时,请使用标准的ISO 8601格式。 These are sensible, practical, and sort chronologically when alphabetical. 这些是明智的,实用的,并且按字母顺序按时间顺序排序。

Use the java.time classes rather than the troublesome old date-time classes that are now legacy. 请使用java.time类,而不要使用现在已成为麻烦的旧的日期时间类。 For Android, see bullets below. 对于Android,请参见下面的项目符号。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( "16/09/2014" , f ) ;

LocalDate dayAfter = ld.plusDays( 1 ) ; 
LocalDate weekAfter = ld.plusWeeks( 1 ) ;
LocalDate monthAfter = ld.plusMonths( 1 ) ;
LocalDate yearAfter = ld.plusYears( 1 ) ;

To generate a string in standard format, simply call toString . 要生成标准格式的字符串,只需调用toString

String output = dayAfter.toString() ;  // YYYY-MM-DD standard format.

2014-09-17 2014-09-17

For other formats, use a DateTimeFormatter as seen above. 对于其他格式,请使用如上所述的DateTimeFormatter

String output = dayAfter.format( f ) ;  

17/09/2014 2014年9月17日


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

Where to obtain the java.time classes? 在哪里获取java.time类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM