简体   繁体   中英

How to calculate one hour ago time from an string array in java which holds value from database

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM