java时间往后一天_如何在Java中将日期增加一天?
24个解决方案
617 votes
像这样的东西应该做的伎俩:
String dt = "2008-01-01"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime()); // dt is now the new date
Dave answered 2018-12-30T16:49:31Z
177 votes
与C#相比,Java似乎远远落后于八球。 此实用程序方法使用Calendar.add方法(可能是唯一简单的方法)显示了在Java SE 6中的方法。
public class DateUtil
{
public static Date addDays(Date date, int days)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days); //minus number would decrement the days
return cal.getTime();
}
}
要根据提出的问题添加一天,请按以下方式调用:
String sourceDate = "2012-02-29";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = format.parse(sourceDate);
myDate = DateUtil.addDays(myDate, 1);
Lisa answered 2018-12-30T16:50:00Z
60 votes
我更喜欢使用Apache的DateUtils。 查看这个[http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html。]它非常方便,特别是当你必须使用它时 您项目中的多个位置,并且不希望为此编写单线方法。
API说:
addDays(Date date,int amount):在返回新对象的日期中添加若干天。
请注意,它返回一个新的Date对象,并且不会对前一个对象进行更改。
Risav Karna answered 2018-12-30T16:50:43Z
58 votes
java.time
在Java 8及更高版本中,java.time包使其非常自动化。(教程)
假设String输入输出:
import java.time.LocalDate;
public class DateIncrementer {
static public String addOneDay(String date) {
return LocalDate.parse(date).plusDays(1).toString();
}
}
Daniel C. Sobral answered 2018-12-30T16:51:18Z
55 votes
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Calendar cal = Calendar.getInstance();
cal.setTime( dateFormat.parse( inputString ) );
cal.add( Calendar.DATE, 1 );
Alex B answered 2018-12-30T16:51:34Z
43 votes
构造一个Calendar对象并使用方法add(Calendar.DATE,1);
krosenvold answered 2018-12-30T16:51:56Z
39 votes
看看Joda-Time([http://joda-time.sourceforge.net/)。]
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime date = parser.parseDateTime(dateString);
String nextDay = parser.print(date.plusDays(1));
Willi aus Rohr answered 2018-12-30T16:52:19Z
37 votes
请注意,这条线增加了24小时:
d1.getTime() + 1 * 24 * 60 * 60 * 1000
但这条线增加了一天
cal.add( Calendar.DATE, 1 );
在夏令时变化(25或23小时)的日子里,您会得到不同的结果!
Florian R. answered 2018-12-30T16:52:54Z
36 votes
Java 8添加了一个用于处理日期和时间的新API。
使用Java 8,您可以使用以下代码行:
// parse date from yyyy-mm-dd pattern
LocalDate januaryFirst = LocalDate.parse("2014-01-01");
// add one day
LocalDate januarySecond = januaryFirst.plusDays(1);
micha answered 2018-12-30T16:53:24Z
25 votes
你可以使用Simple java.util lib
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
cal.add(Calendar.DATE, 1);
yourDate = cal.getTime();
Pawan Pareek answered 2018-12-30T16:53:47Z
22 votes
Date today = new Date();
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1); // number of days to add
String tomorrow = (String)(formattedDate.format(c.getTime()));
System.out.println("Tomorrows date is " + tomorrow);
这将给明天的日期。 c.add(...)参数可以从1更改为另一个数字以获得适当的增量。
Akhilesh T. answered 2018-12-30T16:54:09Z
16 votes
如果您使用的是Java 8,那么请执行此操作。
LocalDate sourceDate = LocalDate.of(2017, Month.MAY, 27); // Source Date
LocalDate destDate = sourceDate.plusDays(1); // Adding a day to source date.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Setting date format
String destDate = destDate.format(formatter)); // End date
如果你想使用SimpleDateFormat,那就这样做吧。
String sourceDate = "2017-05-27"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(sourceDate)); // parsed date and setting to calendar
calendar.add(Calendar.DATE, 1); // number of days to add
String destDate = sdf.format(calendar.getTime()); // End date
Avijit Karmakar answered 2018-12-30T16:54:38Z
15 votes
long timeadj = 24*60*60*1000;
Date newDate = new Date (oldDate.getTime ()+timeadj);
这将从oldDate开始占用epoch以来的毫秒数,并添加1天的毫秒数,然后使用Date()公共构造函数使用新值创建日期。 此方法允许您添加1天或任意数量的小时/分钟,而不仅仅是整天。
dvaey answered 2018-12-30T16:55:02Z
11 votes
由于Java 1.5 TimeUnit.DAYS.toMillis(1)对我来说看起来更干净。
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Date day = dateFormat.parse(string);
// add the day
Date dayAfter = new Date(day.getTime() + TimeUnit.DAYS.toMillis(1));
Jens answered 2018-12-30T16:55:24Z
7 votes
Apache Commons已经有了这个DateUtils.addDays(Date date,int amount)[http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DateUtils.html#addDays% 您使用的是28java.util.Date,%20int%29],或者您可以使用JodaTime使其更清洁。
ROCKY answered 2018-12-30T16:55:47Z
7 votes
只需在String中传递日期和下一天的数量
private String getNextDate(String givenDate,int noOfDays) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String nextDaysDate = null;
try {
cal.setTime(dateFormat.parse(givenDate));
cal.add(Calendar.DATE, noOfDays);
nextDaysDate = dateFormat.format(cal.getTime());
} catch (ParseException ex) {
Logger.getLogger(GR_TravelRepublic.class.getName()).log(Level.SEVERE, null, ex);
}finally{
dateFormat = null;
cal = null;
}
return nextDaysDate;
}
LMK answered 2018-12-30T16:56:09Z
7 votes
如果要添加单个时间单位并且您希望其他字段也增加,则可以安全地使用add方法。 见下面的例子:
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(1970,Calendar.DECEMBER,31);
System.out.println(simpleDateFormat1.format(cal.getTime()));
cal.add(Calendar.DATE, 1);
System.out.println(simpleDateFormat1.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println(simpleDateFormat1.format(cal.getTime()));
将打印:
1970-12-31
1971-01-01
1970-12-31
terrmith answered 2018-12-30T16:56:38Z
7 votes
在java 8中,您可以使用LocalDate
LocalDate parsedDate = LocalDate.parse("2015-10-30"); //Parse date from String
LocalDate addedDate = parsedDate.plusDays(1); //Add one to the day field
您可以按如下方式转换为LocalDate对象。
Date date = Date.from(addedDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
您可以将LocalDate格式化为字符串,如下所示。
String str = addedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Ramesh-X answered 2018-12-30T16:57:13Z
5 votes
使用Calendar API将String转换为Date对象,然后使用Calendar API添加一天。 如果您需要特定的代码示例,请告诉我,我可以更新我的答案。
Ross answered 2018-12-30T16:57:36Z
5 votes
在Java 8中,简单的方法是:
Date.from(Instant.now().plusSeconds(SECONDS_PER_DAY))
dpk answered 2018-12-30T16:57:58Z
4 votes
这很简单,试着用一个简单的词来解释。得到今天的日期如下
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());// print today's date
calendar.add(Calendar.DATE, 1);
现在通过calendar.add方法提前一天设置此日期,该方法采用(常量,值)。 这里的常数可以是DATE,hours,min,sec等,value是常量的值。 就像有一天一样,前面的常量是Calendar.DATE,它的值是1,因为我们想要提前一天的价值。
System.out.println(calendar.getTime());// print modified date which is明天的日期
谢谢
Kushwaha answered 2018-12-30T16:58:41Z
2 votes
如果您使用的是Java 8,java.time.LocalDate和java.time.format.DateTimeFormatter可以使这项工作变得非常简单。
public String nextDate(String date){
LocalDate parsedDate = LocalDate.parse(date);
LocalDate addedDate = parsedDate.plusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd");
return addedDate.format(formatter);
}
realhu answered 2018-12-30T16:59:03Z
2 votes
您可以使用“org.apache.commons.lang3.time”中的此包:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myNewDate = DateUtils.addDays(myDate, 4);
Date yesterday = DateUtils.addDays(myDate, -1);
String formatedDate = sdf.format(myNewDate);
Shaheed answered 2018-12-30T16:59:25Z
-1 votes
Date newDate = new Date();
newDate.setDate(newDate.getDate()+1);
System.out.println(newDate);
Filip Trajcevski answered 2018-12-30T16:59:41Z
java时间往后一天_如何在Java中将日期增加一天?相关推荐
- java 线程中创建线程_如何在Java 8中创建线程安全的ConcurrentHashSet?
java 线程中创建线程 在JDK 8之前,还没有办法在Java中创建大型的线程安全的ConcurrentHashSet. java.util.concurrent包甚至没有一个名为Concurren ...
- java文件中获取创建日期_如何在Java中获取文件的上次修改日期
java文件中获取创建日期 Sometimes we need to get the file last modified date in Java, usually for listeners li ...
- java 查找链表中间元素_如何在Java中一次性查找Java中链表的中间元素
如何在一次传递中找到LinkedList的中间元素?这是一个 Java 和非Java程序员面试时经常被问到的编程问题.这个问题类似于检查回文或计算阶乘,有时也会要求编写代码.为了回答这个问题,候选人必 ...
- java字符串字符排列组合_如何在Java中查找字符串的所有排列
java字符串字符排列组合 In this tutorial, we will learn how to find the permutation of a String in a Java Prog ...
- java 合并两个列表_如何在Java中合并两个列表?
java 合并两个列表 Merging two lists in Java is often a useful operation. These lists can be ArrayLists or ...
- java类添加单元测试代码_如何在java中单元测试时跳过一段代码
如果问题确实是: 如何在Java 然后我给出的答案同意单元测试时,我跳过一段代码.依赖注入,嘲讽框架绝对是真正的单元测试的正确途径. 但是,如果问题是: 使用JUnit(或其他单元测试框架) 然后我想 ...
- java中long如何使用_如何在Java中将long转换为int?
问题 如何在Java中将long转换为int? #1 热门回答(218 赞) 简单类型转换应该这样做: long l = 100000; int i = (int) l; 但请注意,大数(通常大于21 ...
- 在java读字符串入文件_如何在java中将文件读入字符串?
我已经将文件读入String. 该文件包含各种名称,每行一个名称. 现在的问题是我想在String数组中使用这些名称. 为此我写了以下代码: String [] names = fileString. ...
- java如何获得键值_如何在java中取map中的键值 的两种方法
第一种方法根据键值的名字取值 import java.util.HashMap; import java.util.Map; public class Test { /** * @param args ...
最新文章
- vue-router基本使用
- datagridview新增列在最后_数说|科创板2020:募资额2200+亿超主板列A股第一,科技“千元股”、“市值王”长成...
- deepin配置反向代理映射本地到公网
- LVS(14)——DR模型实践、交换机
- 壁纸网站的高清图片,完美符合视觉控的你!
- perl 判断不包含某字符串
- 前端几个常用简单的开发手册拿走不谢
- Leetcode 261.以图判树
- 全军覆没!麻省理工零录取中国学生,斯坦福取消中国大陆面试! 这是怎么了?...
- 计算机动画原理课程设计,Flash动画优化的原理和常用优化方式,毕业论文,课程设计,PPT,开发报告,外文翻译 - 论文助手...
- 2022年最完整的html网页跳转代码大全
- C语言运算符的优先级与结合性
- 网站数据采集器-文章采集工具-关键词文章采集工具
- 镜像构建工具SOURCE TO IMAGE(S2I)实践
- 利用Tensorflow构建RNN并对序列数据进行建模
- 2020年5G通信工程类项目一览,哪些企业成功抢滩?
- python爬虫学习-定制请求头
- 看不见的竞争 带宽优化
- KF、EKF、UKF的matlab代码实现
- win7 删除java_windows7系统卸载java的操作方法?
热门文章
- Matlab符号数学(Symbolic Math with MATLAB)MATLAB解方程
- Andorid AlertDialog 点击后自动消失_不看后悔!2011年别克更换完变速箱电脑后,要如何做设定匹配...
- java 里的 循环不变式 百度百科,循环不变式
- 软件测试2019:第五次作业
- Myeclipse修改设置Default VM Arguments
- bzoj2500幸福的道路 树形dp+单调队列
- phpExcel与jq的ajax
- android Mvp简单实用
- 《Programming with Objective-C》第四章 Encapsulating Data
- 开始使用 Markdown