Java在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
# 测试scanner代码的 发布于:2024-07-26 10:13 去除文件名的后缀<br/> .doc<br/> .docx<br/> .xls<br/> 等 发布于:2024-07-24 14:52 去除字符串中的空格 发布于:2024-07-24 11:38 项目使用测试 发布于:2024-07-23 17:31 Java字符串分割循环 发布于:2024-07-20 17:30 关于商品的链表实现 发布于:2024-07-19 15:19 des加密 发布于:2024-07-17 10:16 学生管理系统 发布于:2024-07-16 15:21 for循环嵌套/打印一个菱形 发布于:2024-07-10 21:20 成绩计算代码 发布于:2024-07-04 20:43 计算到当前时间的天数 发布于:2024-07-03 10:57 java代码演示 发布于:2024-07-02 12:05 小雨买文具 发布于:2024-07-01 21:25 大象喝水c 发布于:2024-07-01 08:52 暂存下csv导出 发布于:2024-06-30 20:18 黑红树,未完成 发布于:2024-06-28 20:49 hihi胡 发布于:2024-06-25 13:39 软件测试用例 发布于:2024-06-23 22:19 张明桐 225060130 发布于:2024-06-23 14:33 多态的介绍 发布于:2024-06-23 13:35 方法重写介绍 发布于:2024-06-23 11:26 --- java窗口 发布于:2024-06-21 16:24 FinallShell激活 发布于:2024-06-21 14:24 分别用三种不同的算法时间复杂度找出不超过n的所有素数,编程实现 这三种不同算法,并对比当n为10万时,三种算法所需要的运行时间 发布于:2024-06-21 08:19 用分支限界法求解0-1背包问题 发布于:2024-06-20 21:27 三角形字符 发布于:2024-06-20 21:30 三角形面积 发布于:2024-06-27 18:25 打印菱形放在一个正方形里面 发布于:2024-06-18 19:53 RSA加密 发布于:2024-06-17 11:59 super()使用细节 发布于:2024-06-14 20:31 继承本质例题 发布于:2024-06-14 19:06 用回溯算法求解 n 皇后问题 发布于:2024-06-13 23:47 用回溯算法求解0-1问题 发布于:2024-06-13 23:45 用回溯算法求解装载问题 发布于:2024-06-13 23:39 用贪心算法实现单源最短路径 发布于:2024-06-13 23:36 用贪心算法构造 Huffman 编码。 发布于:2024-06-13 23:34 1111请问大萨达 发布于:2024-06-21 15:58 用 Prime 贪心算法构造最小生成树 发布于:2024-06-13 11:36 用 Kruskal 贪心算法构造最小生成树 发布于:2024-06-13 11:35 用分治法实现选择第 k 小元素 发布于:2024-06-13 11:27 分治法实现二分查找 发布于:2024-06-13 11:24 求最重和最轻的金块 发布于:2024-06-13 11:22 求公鸡母鸡小鸡 发布于:2024-06-13 11:11 用分治法实现快速排序 发布于:2024-06-13 11:25 背包价值计算 发布于:2024-06-13 11:06 计算下一个日期 发布于:2024-06-08 16:06 11.5(课程类) 发布于:2024-06-07 19:05 11.2(P、S、E、F和S类) 发布于:2024-06-07 18:27 11.1(三角形类) 发布于:2024-06-07 18:25 10.5(显示素数因子) 发布于:2024-06-07 18:02 [更多]
显示目录

Java – How to add days to current date



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

This article shows you how to add days to the current date, using the classic java.util.Calendar and the new Java 8 date and time APIs.

1. Calendar.add

Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current date.

DateExample.java

package com.mkyong.time;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateExample {

    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public static void main(String[] args) {

        Date currentDate = new Date();
        System.out.println(dateFormat.format(currentDate));

        // convert date to calendar
        Calendar c = Calendar.getInstance();
        c.setTime(currentDate);

        // manipulate date
        c.add(Calendar.YEAR, 1);
        c.add(Calendar.MONTH, 1);
        c.add(Calendar.DATE, 1); //same with c.add(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.HOUR, 1);
        c.add(Calendar.MINUTE, 1);
        c.add(Calendar.SECOND, 1);

        // convert calendar to date
        Date currentDatePlusOne = c.getTime();

        System.out.println(dateFormat.format(currentDatePlusOne));

    }

}

Output

2016/11/10 17:11:48
2017/12/11 18:12:49

2. Java 8 Plus Minus

In Java 8, you can use the plus and minus methods to manipulate LocalDate, LocalDateTime and ZoneDateTime, see the following examples

LocalDateTimeExample.java

package com.mkyong.time;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeExample {

    private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
    private static final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    private static final DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(DATE_FORMAT);

    public static void main(String[] args) {

        // Get current date
        Date currentDate = new Date();
        System.out.println("date : " + dateFormat.format(currentDate));

        // convert date to localdatetime
        LocalDateTime localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        System.out.println("localDateTime : " + dateFormat8.format(localDateTime));

        // plus one
        localDateTime = localDateTime.plusYears(1).plusMonths(1).plusDays(1);
        localDateTime = localDateTime.plusHours(1).plusMinutes(2).minusMinutes(1).plusSeconds(1);

        // convert LocalDateTime to date
        Date currentDatePlusOneDay = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        System.out.println("\nOutput : " + dateFormat.format(currentDatePlusOneDay));

    }

}

Output

date : 2016/11/10 17:40:11
localDateTime : 2016/11/10 17:40:11

Output : 2017/12/11 18:41:12

References

  1. Calendar JavaDoc
  2. Java – How to get current date time – date() and calender()
  3. Java 8 – Convert Date to LocalDate and LocalDateTime
由JSRUN为你提供的Java在线运行、在线编译工具
        JSRUN提供的Java 在线运行,Java 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout