0

I have an array of string type which holds values from database which are in the format "2014-09-02 15:19:59.000".Array has 3600 rows .now I want to calculate one hour ago time from these values in java.How to do that. Code for selection for retrieval of values from database is:

public String[] database_Current() {
  List < String > timeStr = new ArrayList < String > ();


  try {
    con = getConnection();
    String sql = "select max(logtime) from vacuum_analog_1mins";

    clstmt = con.prepareCall(sql);

    clstmt.execute();
    rs = clstmt.getResultSet();

    while (rs.next()) {

      timeStr.add(rs.getString(1).substring(11, 16));

    }

  } catch (Exception e) {
    System.out.println("\nException in  Bean in getDbTable(String code):" + e);
  } finally {
    closeConnection();
  }
  // I would return the list here, but let's convert it to an array
  atime = timeStr.toArray(new String[timeStr.size()]);





  return atime;
}
How to get one hour time ago from atime values.
SRY_JAVA
  • 323
  • 3
  • 10
  • 21
  • the date/time should have been stored in milliseconds, measured in milliseconds between the current time and midnight, January 1, 1970 UTC . Also see this ans: http://stackoverflow.com/a/12/3879470 – Mohammed Ali Nov 24 '14 at 06:25

2 Answers2

2

Your question is little bit confusing. In your question, you are getting only single row from query "select max(logtime) from vacuum_analog_1mins" that contains max(logtime) single column and that single value you are storing in timeStr array. I didn't get what the purpose of using array here to store single value even if you will get single value every time.

What i got is you want to have time one hour less than actual time getting from database.

example:

getting from database:

currentDateFromDB = 2014-09-02 15:19:59.000

you want to have:

changedDate = 2014-09-02 14:19:59.000

For This you can use Calendar class of java as:

//currentDateFromDB in string form from db
Date parsedDate = new Date();
try {
    SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    parsedDate = format.parse(currentDateFromDB);
}
catch(ParseException pe) {
     throw new IllegalArgumentException();
}

Calendar calendar=Calendar.getInstance();
calendar.setTime(parsedDate); //set date here
int hours = calendar.get(Calendar.HOUR_OF_DAY);
hours--; //reduce by one
calendar.set(Calendar.HOUR_OF_DAY, hours); //again set desired hours
Date date=calendar.getTime(); //get modified time
kriyeta
  • 695
  • 4
  • 12
  • My values from database are in array.How to convert this array into calender.Moreover when I do as u did it ,then the following error is shown 'The method set(int, int) in the type Calendar is not applicable for the arguments (int)' and the The constructor Date(String) is deprecated. – SRY_JAVA Nov 24 '14 at 06:26
  • when I do as u said then one hour previous time is shown but there is some mistake in date parameter,My out put is: _'previous date of db is Thu Jan 02 15:19:59 IST 2014 current date of db is 2014-09-02 16:19:59.0'_.I want previous date also in the same format as current database date. – SRY_JAVA Nov 24 '14 at 09:38
  • @SRY_JAVA You can use SimpleDateFormat(format) to format your date as you want. – kriyeta Nov 24 '14 at 10:32
  • I have used SimpleDateFormat(format).Some of my code is :_'String format = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); Date date = simpleDateFormat.parse(timestr); Calendar calendar=Calendar.getInstance(); calendar.setTime(date); System.out.println("previous date is"+(fixedDate)); System.out.println("current date is"+timestr); return fixedDate;'_. But then also my output is:_'previous date isTue Sep 02 15:19:59 IST 2014 current date is 2014-09-02 16:19:59.0'_.How to change the format of previous date. – SRY_JAVA Nov 24 '14 at 11:17
1

I prefer using Joda DateTime for manipulating dates, here's a debate about it https://softwareengineering.stackexchange.com/questions/190891/joda-time-vs-java-time

for what concerns your question, the following snippet:

String sDate = "2014-09-02 15:19:59.000";
String format = "yyyy-MM-dd HH:mm:ss.SSS";
DateTime oDate = DateTime.parse(sDate,
        DateTimeFormat.forPattern(format));
System.out.println( oDate.minusHours(1).toString(format));

prints

2014-09-02 14:19:59.000

the imports

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

If you cannot use DateTime, the solution using only java.util would be as kriyeta suggests, just some fixes:

String sDate = "2014-09-02 15:19:59.000";
String format = "yyyy-MM-dd HH:mm:ss.SSS";    
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date = simpleDateFormat.parse(sDate);
Calendar calendar=Calendar.getInstance();
calendar.setTime(date); 
int hours = calendar.get(Calendar.HOUR_OF_DAY);
hours--; 
calendar.set(Calendar.HOUR_OF_DAY, hours);
Date fixedDate=calendar.getTime(); 
System.out.println(simpleDateFormat.format(fixedDate));

With respect to the comment, to have the output look the same you need to align the formats so either

System.out.println(date + "-" + fixedDate); // Tue Sep 02 15:19:59 CEST 2014-Tue Sep 02 14:19:59 CEST 2014

or

 System.out.println(simpleDateFormat.format(date) + "-" + simpleDateFormat.format(fixedDate)); // 2014-09-02 15:19:59.000-2014-09-02 14:19:59.000

but you have a complete control over how the dates are represented as strings, so don't let that confuse you, the only important thing is to get your Date instances right

Community
  • 1
  • 1
Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • I'm using Eclipse IDE .When I import then message that The import org.joda cannot be resolved is shown. – SRY_JAVA Nov 24 '14 at 06:42
  • yes its a third-party lib, you would have to download joda-datetime jar and place on classpath, added a java util solution only as well, just in case you don't won't to use third-party libs – Master Slave Nov 24 '14 at 06:50
  • @MasterSlave you shouldn't give same answer. Think something different. – kriyeta Nov 24 '14 at 06:52
  • :) the same but fixed you'r bug, setHours, getHours, and the wrong format, anyways, a vote-up for you and a plee to SRY_JAVA to accept yours, if it fixes his issue :) – Master Slave Nov 24 '14 at 06:55
  • @MasterSlave okay. no probs. – kriyeta Nov 24 '14 at 06:58
  • @MasterSlave when I do as u said then time is showing one hour previous time but there is some mistake in date parameter,My out put is: _"previos date odf db is Thu Jan 02 15:19:59 IST 2014 current date odf db is 2014-09-02 16:19:59.0 "_.I want previous date also in the same format as current database date. – SRY_JAVA Nov 24 '14 at 09:21