?
Solved

calculating timestamp operations

Posted on 2013-11-20
8
Medium Priority
?
208 Views
Last Modified: 2013-12-10
hi guys

I have this code

I am trying to get the currentTime at which my program runs and want to get 7 hours prior to that


DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
Calendar cal = Calendar.getInstance();  
String currentRunningTime = dateFormat.format(cal.getTime()); //current time at which my program runs
String sevenHoursPreviousTime = "";

Open in new window

so if currentRunningTime =  2013-11-19 13:21:55.027
i want to subtract 7 hours from currentRunningTime.

Any idea how i can do that?
Any help would be appreciated.

thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 568 total points
ID: 39663400
You want something like this:

Calendar cal =       cal.add(Calendar.HOUR_OF_DAY, -7) ;      // 7 hours ago

Doug
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 568 total points
ID: 39663417
Try

	Calendar c = Calendar.getInstance();
	c.add(Calendar.HOUR, -7);
	String sevenEarlier = new java.sql.Timestamp(c.getTime().getTime()).toString();
	System.out.println(sevenEarlier);

Open in new window

0
 
LVL 14

Assisted Solution

by:jb1dev
jb1dev earned 564 total points
ID: 39663852
As Doug suggests, you can use the calendar class.

You can also do it without making use of the Calendar class by calculating the milliseconds yourself.

E.g.
import java.util.*;
import java.text.*;

public class Time {
    
    public static void main(String[] args) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String currentRunningTime = dateFormat.format(System.currentTimeMillis());
        String sevenHoursPreviousTime = dateFormat.format(
                    System.currentTimeMillis() - 
                    (1000 * 60 * 60 * 7) );

        System.out.println(currentRunningTime);
        System.out.println(sevenHoursPreviousTime);

    }

}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:jb1dev
ID: 39663859
Also, I would highly recommend against using the java.sql.Timestamp class if you are not working directly with SQL in some kind of persistence layer.
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39663870
Sorry, it is more accurate to call the System.currentTimeMillis() function only once (otherwise you are off by a few milliseconds)
This is better.

import java.util.*;
import java.text.*;

public class Time {
    
    public static void main(String[] args) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        long currentTimeMillis = System.currentTimeMillis();
        String currentRunningTime = dateFormat.format(currentTimeMillis);
        String sevenHoursPreviousTime = dateFormat.format(
                    currentTimeMillis - (1000 * 60 * 60 * 7) );

        System.out.println(currentRunningTime);
        System.out.println(sevenHoursPreviousTime);

    }

}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39663913
Also, I would highly recommend against using the java.sql.Timestamp class if you are not working directly with SQL in some kind of persistence layer.
Why is that?
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39663974
Why would you want to use java.sql.* classes outside of the context of JDBC?

This is a great way to introduce bugs when someone has a java.util.Date and they want to compare against a java.sql.Timestamp (which is a subclass and could be passed around as the base class, hiding it's true type from the user.)

From the javadoc:
http://docs.oracle.com/javase/7/docs/api//java/sql/Timestamp.html

Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. The fractional seconds - the nanos - are separate. The Timestamp.equals(Object) method never returns true when passed an object that isn't an instance of java.sql.Timestamp, because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method. Also, the hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation.

Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39664021
Why would you want to use java.sql.* classes outside of the context of JDBC?
Because it's a smple tool to do a simple job - namely to get a timestamp as a String in the format that royjayd needs. Your observations on type complexity do not seem to be relevant to this case
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question