0

I am new to Java development - and I am trying to convert a date value - "2017-09-14" to a timestamp like "1355759900000".

I've tried to get a conversion like this

Date starting_time = (Date) obj.get("assessment date");
java.sql.Timestamp timeStampDate = new Timestamp(starting_time.getCreatedTime().getTime());
System.out.println("Today is " +timeStampDate);

but it tells me Timestamp is not a resolved type.

I also need to do a calculation where I create an end date -- a day in the future from the start date.


got it working after this

//get start time date
Date starting_time = (Date) obj.get("assessment date");
//get start time date

//get end time date
Calendar c = Calendar.getInstance(); 
c.setTime(starting_time); 
c.add(Calendar.DATE, 1);
java.util.Date ending_time = c.getTime();
//get end time date

long starting_time_timestamp = new Timestamp(starting_time.getTime()).getTime();
long ending_time_timestamp = new Timestamp(ending_time.getTime()).getTime();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

1

Creating timestamp:

Timestamp timestamp = new Timestamp(date.getTime());

1 day in future

MolecularMan
  • 227
  • 2
  • 16
0

How are you converting "2017-09-14" to a timestamp like "1355759900000".

I did not get this one, but here is the code that you can use.

Output is :

Today is 2015-03-31 00:00:00.0

String timestamp: 31032015000000

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class test {

    public static void main(String[] args) throws IOException, InterruptedException {
        String str="2015-03-31";
        Date starting_time = Date.valueOf(str);
        java.sql.Timestamp timeStampDate = new Timestamp(starting_time.getTime());
        System.out.println("Today is " + timeStampDate);
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyHHmmss");
        System.out.println("String timestamp: "+sdf.format(timeStampDate));
    }
}
Abhishek Honey
  • 645
  • 4
  • 13