Search code examples
javadatetimejfreechartdate-formatjava-time

How to convert Java's date strings/objects into JFreeChart's Date format?


I'm creating a candlestick chart using the JFreeChart library. I need to pass time values into OHLCSeries, but it only accepts its own time values

I'm going to pass this class https://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/Hour.html

And this constructor: Hour(int hour, int day, int month, int year)

The data which I get from the API originally looks like this: 2021-03-02T16:00:00.000Z

My question: how to convert this date string/Java time formats into JFreeChart's Date format? I can only think about some complicated converting into Java types + returning each value separately, or using regexes


Solution

  • You can parse the date-time string into Instant and then get java.util.Date object from this instant. Using this java.util.Date object, you can create an instance of Hour.

    String strDateTime = "2021-03-02T16:00:00.000Z";
    Instant instant = Instant.parse(strDateTime);
    Hour hour = new Hour(Date.from(instant)));