• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Getting Compile Error with Date Function

 
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting a compile error(line 10): Type Mismatch: Cannot convert from String to Date
I thought both sides of the equation would be Date type.

 
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
format() returns a String.
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:format() returns a String.



First thanks for looking at this.

I have changed my code to:



I get this in console at run time. Strange that it is the cal.getTime() is returning a different date than I have set:

FAILocationLogLineItemV8: exception: Tue Jan 31 08:44:34 CST 2040 Tue Jan 31 08:44:34 CST 2040
java.text.ParseException: Unparseable date: "Tue Jan 31 08:44:34 CST 2040"
at java.text.DateFormat.parse(DateFormat.java:369)
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:

Stephan van Hulst wrote:format() returns a String.



First thanks for looking at this.

I have changed my code to:



I get this in console at run time. Strange that it is the cal.getTime() is returning a different date than I have set:

FAILocationLogLineItemV8: exception: Tue Jan 31 08:44:34 CST 2040 Tue Jan 31 08:44:34 CST 2040
java.text.ParseException: Unparseable date: "Tue Jan 31 08:44:34 CST 2040"
at java.text.DateFormat.parse(DateFormat.java:369)



I just realized the month is off by 1(Jan = 0). But still have Unparseable date issue.
 
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:I just realized the month is off by 1(Jan = 0). But still have Unparseable date issue.


Your pattern is: dd/MM/yyyy

but the date string returned by cal.getTime().toString() is something like: Sat Dec 31 00:00:00 PST 2039
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Steve Dyke wrote:I just realized the month is off by 1(Jan = 0). But still have Unparseable date issue.


Your pattern is: dd/MM/yyyy

but the date string returned by cal.getTime().toString() is something like: Sat Dec 31 00:00:00 PST 2039



Thanks.

I have modified my code and now am getting a date but not the format I need:



Produces date with this format(unchanged from original format): Sat Dec 31 00:00:00 CST 2039
 
Ron McLeod
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:Produces date with this format(unchanged from original format): Sat Dec 31 00:00:00 CST 2039


Are you wanting to parse or format?
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Steve Dyke wrote:Produces date with this format(unchanged from original format): Sat Dec 31 00:00:00 CST 2039


Are you wanting to parse or format?



In the end I need a Date formatted like 12/31/2039

Since cal.getTime() returns a string like Sat Dec 31 00:00:00 PST 2039, would I need to format this this to 12/31/2039
But since it is still a string
Wouldn't I do a parse to cast it to a date?
 
Ron McLeod
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:In the end I need a Date formatted like 12/31/2039


If I understand correctly, then there is no need to perform any parsing, you would simply format the Date type representation:
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Steve Dyke wrote:In the end I need a Date formatted like 12/31/2039


If I understand correctly, then there is no need to perform any parsing, you would simply format the Date type representation:



This throws a:
Type mismatch: cannot convert from String to Date

Thought I was over complicating this so changed code to:



However, still returns undesired format: Sat Dec 31 00:00:00 CST 2039
 
Ron McLeod
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This what I understand you are trying to do:

Code:
Console:
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:This what I understand you are trying to do:

Code:
Console:



Sorry duedate is a date not a string.
 
Ron McLeod
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:Sorry duedate is a date not a string.



It seems like I don't understand your end goal.  If you want a Date object then it would be something like this:No need to parse or format anything.
 
Ron McLeod
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:In the end I need a Date formatted like 12/31/2039


That is a formatted String representation of a Date object - not a Date object.
 
Steve Dyke
Ranch Hand
Posts: 2155
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Steve Dyke wrote:Sorry duedate is a date not a string.



It seems like I don't understand your end goal.  If you want a Date object then it would be something like this:No need to parse or format anything.



Thanks, that works great but how do I get it into the 12/31/2039 format?
 
Ron McLeod
Sheriff
Posts: 4006
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Dyke wrote:Thanks, that works great but how do I get it into the 12/31/2039 format?


It feels like things are just going around in circles.

Forgetting about any particular solution - what are you actually trying to accomplish?
 
Bartender
Posts: 1899
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Calendar class is not used that much anymore AFAIK.

Take a look at these examples:

https://howtodoinjava.com/java/date-time/java8-datetimeformatter-example/

Using the DateTimeFormatter, and the Java 8 DateTime API, there's virtually nothing you can't do.

Here's an example using your date. As you can see, you can input the date in any format -- no calendar object required.


Code prints: Desired Date in mm/dd/yyyy: 12/31/2039

The Java 8 Date API rocks!!!

HTH

-- mike
 
Stephan van Hulst
Saloon Keeper
Posts: 14488
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is though that Steve has difficulties differentiating a date's internal value from its formatted value.

Steve, an instance of Date doesn't keep track of a format. It's not a string value, so it has no use for a format. Formats are properties of strings.

Just like how the integer value 'twelve' can be formatted as "12", "0b1010", "0x0c" or one of an infinite number of alternatives, the date value 'December thirty-first, two-thousand-and-thirty-nine' can be formatted as "12/31/2039", "Sat Dec 31 00:00:00 PST 2039", or also an infinite number of alternatives. Once you parse such a formatted value back to a strongly typed value, the object doesn't know what format the string had before it was parsed. It just consists of some internal binary representation.

Converting a string (NOT a date) from one format to another usually means parsing the string to a strongly typed value, and then formatting the strongly typed value to a different string.
 
Your mother is a hamster and your father smells of tiny ads!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic