Use of the Java Timestamp class

Use of the Java Timestamp class

How do I convert a java.util.Date type to a java.sql.Timestamp type?

  Simply, we can declare Timestamp TS = new Timestamp (new Date (). getTime ()); so we can get a more specific type conversion for time!!! 

In developing web applications, we need to do a variety of conversion for different database date types in our programs.If the corresponding database data is of type Date of oracle, that is, only the year, month and day, you can choose to use the type java.sql.Date. If the corresponding database data is of type DateTime of MSsqlserver, that is, the type java.sql.Timestamp is needed for the year, month, day, hour and second. 

You can use the dateFormat to define the format of the time and date by converting it to a string

package personal.jessica; 
import java.util.Date; 
import java.util.Calendar; 
import java.sql.Timestamp; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Locale; 
class Datetest{ 
/** 
  *method Converts a date of type string to a timestamp (timestamp java.sql.Timestamp) 
   dateString String that needs to be converted to timestamp 
   dataTime timestamp 
  */ 
public final static java.sql.Timestamp string2Time(String dateString) 
  throws java.text.ParseException { 
   DateFormat dateFormat; 
  dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//Format 
  //dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH); 
  dateFormat.setLenient(false); 
  java.util.Date timeDate = dateFormat.parse(dateString);//util type 
  java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp type, timeDate.getTime() returns a long type 
  return dateTime; 
} 
/** 
  *method Converts a date of string type to a Date (java.sql.Date) 
   dateString String that needs to be converted to Date 
   dataTime Date 
  */ 
public final static java.sql.Date string2Date(String dateString) 
  throws java.lang.Exception { 
  DateFormat dateFormat; 
  dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); 
  dateFormat.setLenient(false); 
  java.util.Date timeDate = dateFormat.parse(dateString);//util type 
  java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql type 
  return dateTime; 
} 

public static void main(String[] args){ 
  Date da = new Date(); 
  //Note: da.getTime() here gets a long value 
  System.out.println(da.getTime()); 

  //Convert from date date to timestamp 

  //First method: use new Timestamp(long) 
  Timestamp t = new Timestamp(new Date().getTime()); 
  System.out.println(t); 

  //Second method: use Timestamp(int year,int month,int date,int hour,int minute,int second,int nano) 
  Timestamp tt = new Timestamp(Calendar.getInstance().get( 
      Calendar.YEAR) - 1900, Calendar.getInstance().get( 
      Calendar.MONTH), Calendar.getInstance().get( 
      Calendar.DATE), Calendar.getInstance().get( 
      Calendar.HOUR), Calendar.getInstance().get( 
      Calendar.MINUTE), Calendar.getInstance().get( 
      Calendar.SECOND), 0); 
  System.out.println(tt); 

  try { 
   String sToDate = "2005-8-18";//String used to convert to java.sql.Date 
      String sToTimestamp = "2005-8-18 14:21:12.123";//String used to convert to java.sql.Timestamp 
      Date date1 = string2Date(sToDate); 
      Timestamp date2 = string2Time(sToTimestamp); 
   System.out.println("Date:"+date1.toString());//The results show that 
   System.out.println("Timestamp:"+date2.toString());//The results show that 
  }catch(Exception e) { 
   e.printStackTrace(); 
  } 
} 
} 

How to handle a time period, such as when someone logs in for xx days, xx hours, xx minutes, xx seconds

This problem can be dealt with as follows:

First of all, you can definitely read the exact time this user first logged on from the database:

May as well be 2002-01-01 12:00:00

Similarly, when he last landed so far:

May as well be 2002-09-08 13:14:15

If the data is of a time type, no conversion is required, and if it is a String, post-processing can be done.

You can use an example like the one above to get the Timestamp value dateTime for these two times

Then use the dateTime.getTime() method to get the longs of these two times, subtract them (big decreases, that is, subtract them first by time), and get a longs, which is calculated in milliseconds. If you convert it, you will know how many days, hours, minutes and seconds it is.

For example, first divide (in java / divide) the milliseconds of a day to get the day, then divide the remaining values by the milliseconds of an hour to get the hour...At the end of the day, you get all the needs

Keywords: Java SQL Database Oracle

Added by domainbanshee on Sun, 30 Jun 2019 19:33:12 +0300