3

Sorry im new to java, may i know how can i add extra time in here?

SimpleDateFormat timestampFormat    = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
String currTimestamp  = timestampFormat.format(new Date());
System.err.println("currTimestamp=="+currTimestamp); //  2014/10/17 14:31:33
user3835327
  • 1,194
  • 1
  • 14
  • 44
  • 1
    `new Date(System.currentTimeMillis() + 3000 * 60)` – morgano Oct 17 '14 at 06:46
  • Hints - use [Calendar](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) – Subhrajyoti Majumder Oct 17 '14 at 06:46
  • @BasilBourque see title, differentiate between date & time. – user3835327 Oct 17 '14 at 09:26
  • @user3835327 (a) So if the answer to adding a day is calling `plusDays()` (Joda-Time) or `.add(Calendar.DATE, 1)` (java.util.Date), the answer to adding hours or minutes might be… (b) your question is unclear as you fail to define "extra time". (c) Adding and subtracting time in Java has been covered in *hundreds* of answers. Please search before posting. Or at least bother to look at the questions suggested while you were posting yours, or even now the "Related" questions listed on the right of this web page. – Basil Bourque Oct 17 '14 at 16:11

4 Answers4

6

You can use Calender for this.

Calendar calendar=Calendar.getInstance(); // current time
System.out.println(calendar.getTime());
calendar.add(Calendar.MINUTE,3); // add 3 minutes to current time
System.out.println(calendar.getTime());

Out put:

Fri Oct 17 12:17:13 IST 2014
Fri Oct 17 12:20:13 IST 2014
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
4

Just as a comparison, using Java 8's new time API...

LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)));
ldt = ldt.plusMinutes(3);
System.out.println(ldt.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)));

Or if you can't use Java 8, you could use the JodaTime API

SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateTime dt = DateTime.now();
System.out.println(timestampFormat.format(dt.toDate()));
dt = dt.plusMinutes(3);
Date date = dt.toDate();
System.out.println(timestampFormat.format(dt.toDate()));
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Calander class have some useful methods to do this. If you want to still use Date it self, Add 3000 milliseconds to the current time.

String resultTime = timestampFormat.format(new Date(new Date().getTime() + 3000));
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • This can be really dangerous around certain time boundaries as it doesn't take into consideration things like leap-seconds. Sure over short ranges, it might not be "bad" but it encourages bad habits where better approaches exist - just saying... – MadProgrammer Oct 17 '14 at 07:07
  • @MadProgrammer Point taken. Got that :) – Suresh Atta Oct 17 '14 at 07:50
2

It would be better to use the Calendar class instead of using deprecated Date class:

Pull a Calendar instance:

Calendar c = Calendar.getInstance();

Add 3 minutes to the calendar current time:

c.add(Calendar.MINUTE, 3);

Format the new calendar time:

SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
String currTimestamp = timestampFormat.format(c.getTime());
System.err.println("currTimestamp==" + currTimestamp);
tmarwen
  • 15,750
  • 5
  • 43
  • 62