-5

I am using below condition to get current system time as a start time and tomorrow time as stop time in my tracing logs project. But now I want starttime should be +1mins to the current system time. Can anybody help with this?

SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String startTime=date_format.format(new Date());
String tomorrowTime=date_format.format(new Date(new Date().getTime()+(1000*60*60*24)));

Thanks

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
swa
  • 1
  • 1
  • 3

4 Answers4

7
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1);

Date currentTimePlusOneMinute = cal.getTime();
JosefN
  • 952
  • 6
  • 8
  • Actually, involving the Calendar for this straightforward job is a bit of an overkill. Minutes are a homogeneous unit, unlike months or years, where you really do need the help of the full Calendar. – Marko Topolnik Dec 17 '13 at 21:22
2
String startTime=date_format.format(new Date(System.currentTimeMillis() + 60L * 1000L));
SiN
  • 3,704
  • 2
  • 31
  • 36
1

tl;dr

Instant.now()
       .plusSeconds( 60 )

java.time

The modern way is with the java.time classes.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instantOneMinuteLater = Instant.now().plusSeconds( 60 );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Joda-Time

Update: The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

In Joda-Time 2.3.

// import org.joda.time.*;

DateTime minuteLater = DateTime.now().plusMinutes( 1 );
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
Calendar afterOneMinute = Calendar.getInstance();
afterOneMinute.add(Calendar.MINUTE, 1);
Calendar afterOneDay = Calendar.getInstance();
afterOneDay.add(Calendar.DAY, 1);

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(isoFormat.format(afterOneMinute.getTime()) + " to " + isoFormat.format(afterOneDay.getTime()));
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110