0

IP:

test: String = 2018-10-12 07

Expected OP:

test_timestamp: Timestamp = 2018-10-12 07

Code:

val format1 = new SimpleDateFormat("yyyy-MM-dd HH")
var d = format1.parse(test)
var date =    new Date(d.getTime())
var timestamp = new Timestamp(d.getTime())

My OP:

timestamp: java.sql.Timestamp = 2018-10-12 07:00:00.0

I am trying to convert to the same datehour format.

Code:

var dateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
timestamp = dateFormat.format(timestamp);

Error:

found   : String
required: java.sql.Timestamp
timestamp = dateFormat.format(timestamp);
                                ^

the variable "timestamp" is clearly of type java.sql.Timestamp, so I am not able to understand the issue. Any suggestions.

data_person
  • 4,194
  • 7
  • 40
  • 75
  • 1
    If you know that `timestamp` *"is clearly of type java.sql.Timestamp"*, then why are you trying to assign a `String` to a variable of type java.sql.Timestamp? – Andrey Tyukin Oct 16 '18 at 18:03
  • @AndreyTyukin I am trying only to convert the format of the timestamp to the datehour. Not sure how this is assigning a string. Could you please explain. – data_person Oct 16 '18 at 18:06
  • 1
    The variable `timestamp` is of type `java.sql.Timestamp`. The output of `dateFormat.format(...)` is of type `String`. You cannot assign a `String` to a variable of type `java.sql.Timestamp`, because `String` is not a subtype of `java.sql.Timestamp`. – Andrey Tyukin Oct 16 '18 at 18:07
  • @AndreyTyukin Got it. Any suggestions on how to format the timestamp without converting it back to String. – data_person Oct 16 '18 at 18:09
  • 1
    *"format"* and *"without converting [...] to String"* is self-contradictory. Just pick *any other variable name* that is not "timestamp" to hold the `String` result. – Andrey Tyukin Oct 16 '18 at 18:10
  • @AndreyTyukin Got it. – data_person Oct 16 '18 at 18:12
  • @AndreyTyukin what I meant was how to change the representation of the timestamp to 'yyyy-MM-dd HH' without converting its type to string – data_person Oct 16 '18 at 18:14
  • In this case I don't understand what you mean by "representation". What other non-`String` representation do you want? As byte array? As barcode? Could you please clarify the question, I'm not sure what exactly is being asked. The error message in the current version of the question stems from the attempt to reassign a `String` to a variable of type `java.sql.Timestamp`, this doesn't seem to be what you are asking in your last comment. – Andrey Tyukin Oct 16 '18 at 18:17
  • @AndreyTyukin. i have given that in the expected OP. When I am trying to convert a String to Timestamp, the format I need is "yyyy-MM-dd HH", but the op after converting to timestamp is "yyyy-MM-dd HH:mm:SS". Any suggestions on how I can remove the minutes and seconds from the timestamp – data_person Oct 16 '18 at 18:21
  • `2018-10-12 07:00:00.0` is not a properly formatted `String`, it's just the output of `Timestamp`s `toString` method that should be used for debugging purposes only, and shouldn't be taken too seriously. You should not care how `Timestamp`s are displayed in the REPL, and you should not rely on any particular format used by `toString`. Since you already seem to know how to obtain a String in the format that you actually *do* want, I'm not sure what there's left to answer. – Andrey Tyukin Oct 16 '18 at 18:26
  • By the way: [here is the code that generates the "2018-10-12 07:00:00.0"-output](https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/classes/java/sql/Timestamp.java#L350). There is no way to configure it. The only way to change it in the REPL would be to recompile the Java standard library, or tweak some inner details of the REPL itself. But, again, this is just the `toString` output that shouldn't be used for anything but debugging. – Andrey Tyukin Oct 16 '18 at 18:34
  • @AndreyTyukin Thanks. Got it. – data_person Oct 16 '18 at 18:36
  • 1
    @OleV.V. Good duplicate candidate, unfortunately I can't dupe-hammer it, because I've already voted to close as "trivial typo". – Andrey Tyukin Oct 16 '18 at 19:10
  • There are many similar questions asking for a `java.util.Date`, a `Calendar`, a `LocalDate`, a `java.sql.Date` with a particular format. I don’t know if there’s already one asking for a `Timestamp` with a particular format, probably somewhere. In any case, the issue is the same as in this question, and the answer is the same too. Thanks, @AndreyTyukin, for supporting that this is a duplicate. – Ole V.V. Oct 16 '18 at 19:21
  • @OleV.V. Yes there maybe someone asking for a particular format, that could be because the headings of the question would have been something different and it wouldn't have shown up in the search. In any case, you can probably try changing the headings for all the questions in this category so that next time we wont ask for a particular format:) – data_person Oct 16 '18 at 20:11

0 Answers0