2

I want to convert string date to Cassandra time stamp format

Example date String inputDate="20170525"

Vigneshwaran
  • 229
  • 1
  • 3
  • 14

2 Answers2

0

You need to convert your string to Date.
Java Date type maps cassandra timestamp type

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = dateFormat.parse("20170525");

Now you have the date you can insert or query with it in prepared statement

Note : You don't have any timezone. So default timezone will be used. If you want to specify the timezone use dateFormat.setTimeZone(TimeZone zone) method

Ashraful Islam
  • 12,470
  • 3
  • 32
  • 53
  • 1
    You are ignoring the crucial issue of time zone. You are creating a value representing the first moment of the day in UTC. This would be incorrect if the context of the date were meant for Montreal or Los Angeles etc. – Basil Bourque May 23 '17 at 01:06
0

First, parse that input string as a LocalDate.

LocalDate ld = LocalDate.parse( "20170525" , DateTimeFormatter.BASIC_ISO_DATE ) ;

For a date-only value without time-of-day, you should be using type Date in Cassandra according to this documentation.

You can exchange data as strings using standard ISO 8601 format. The java.time classes use the standard formats by default. So no need to specify a formatting pattern.

String output = ld.toString() ;

2017-05-25

If you really want to store in the timestamp, you must specify a time-of-day. Perhaps you want the first moment of the day. Determining that specific moment on the timeline that requires a time zone. Do not assume the first moment occurs at 00:00:00. Anomalies such as Daylight Saving Time mean the time may be another value such as 01:00:00.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;

zdt.toString(): 2017-05-25T00:00:00-04:00[America/Montreal]

Cassandra stores the Timestamp field only as UTC. So we need to adjust the ZonedDateTime from our desired time zone to UTC. The easiest way to do that is extract a Instant. The Instant class is always in UTC by definition.

Instant instant = zdt.toInstant() ;

Generate a string in standard ISO 8601 format for Cassandra. Notice how the hour jumps from zero to four. Our time zone America/Montreal is four hours behind UTC on that date. So getting to UTC means adding four hours, 0 + 4 = 4.

String output = instant.toString() ;

2017-05-25T04:00:00Z

Going the other way when your retrieve this value.

Instant instant = Instant.parse( "2017-05-25T04:00:00Z" ) ;
ZonedDateTime zdt = instant.atZone( z );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154