1

Good day, dear colleagues.

I have data with UTC datetime. While creating the chart, I'd like to convert time to Local time with respecting the timezone set on host machine. Is there a way to do this for TimeSeries?

private XYDataset createDataset(Set<Parameters> parameters) {
        var s1 = new TimeSeries("Series1 title");
        for (Parameters p : parameters) {
            s1.addOrUpdate(new Second(p.getDatetime()), p.getTVol()); // <-- p.getDateTime() returns java.util.Date in UTC
        }
        var dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        return dataset;
    }
meridbt
  • 304
  • 1
  • 11

2 Answers2

1

As you've observed, your chosen Second constructor accepts a java.util.Date that reflects an instant in coordinated universal time (UTC). An alternative to this approach is to defer the conversion from UTC to local time until the value is displayed in the view. As your time series chart likely uses a DateAxis for the domain, you will find that local times are displayed by default; use setDateFormatOverride() to change the default display. In this example, the axis is conditioned to display UTC:

DateAxis axis = (DateAxis) plot.getDomainAxis();
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ssX");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
axis.setDateFormatOverride(df);

For comparison, hover over a data point to see the tooltip, which displays the corresponding time in the host platform's local time. Additional examples may be found here.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Well, I closed the issue by the following trick:

private Date convertUtcToLocalDateTime(Date date) {
        var dtfUtc = new SimpleDateFormat(dateTimeFormat);
        dtfUtc.setTimeZone(TimeZone.getTimeZone("0"));
        var dtfLocal = new SimpleDateFormat(dateTimeFormat);
        try {
            return dtfUtc.parse(dtfLocal.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

If someone could propose better solution - I'd be grateful

meridbt
  • 304
  • 1
  • 11