view src/main/java/nl/cwi/monetdb/mcl/connection/helpers/TimestampHelper.java @ 277:4face9f42efc embedded

Merge with default.
author Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
date Thu, 18 Jul 2019 11:22:55 +0200 (2019-07-18)
parents 5b13ccaba741
children
line wrap: on
line source
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
 */

package nl.cwi.monetdb.mcl.connection.helpers;

import java.sql.Timestamp;
import java.util.Calendar;

/**
 * Due to the poor design of the old Java date and time API, when retrieving timestamps from the MAPI connection, this
 * class is used to store the calendar with timezone information and the nanoseconds information to generate a
 * {@link Timestamp} instance, as there is no mapping in Java Classes to store both information.
 *
 * @author Pedro Ferreira
 */
public class TimestampHelper {

	/** The calendar instance */
	private Calendar calendar;

	/** The nanoseconds information */
	private int nanoseconds;

	TimestampHelper(Calendar calendar, int nanoseconds) {
		this.calendar = calendar;
		this.nanoseconds = nanoseconds;
	}

	/**
	 * Gets the Calendar instance.
	 *
	 * @return The Calendar instance
	 */
	public Calendar getCalendar() {
		return calendar;
	}

	/**
	 * Sets the Calendar instance.
	 *
	 * @param calendar The Calendar instance
	 */
	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	/**
	 * Gets the nanoseconds information.
	 *
	 * @return The nanoseconds information
	 */
	public int getNanoseconds() {
		return nanoseconds;
	}

	/**
	 * Sets the nanoseconds information.
	 *
	 * @param nanoseconds The nanoseconds information
	 */
	public void setNanoseconds(int nanoseconds) {
		this.nanoseconds = nanoseconds;
	}

	/**
	 * Generates a {@link Timestamp} instance from the provided information.
	 *
	 * @return The generated {@link Timestamp} instance
	 */
	public Timestamp getTimestamp() {
		Timestamp res = new Timestamp(calendar.getTimeInMillis());
		res.setNanos(nanoseconds);
		return res;
	}
}