All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wicket.util.time.AbstractTime Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.util.time;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Abstract base class for subclasses that represent a point in time (as opposed to a
 * {@link Duration} of time).
 * 
 * @author Jonathan Locke
 * @since 1.2.6
 */
abstract class AbstractTime extends AbstractTimeValue
{
	private static final long serialVersionUID = 1L;

	/** calendar for the local time zone */
	static final Calendar localtime = Calendar.getInstance();

	/** time format */
	static final SimpleDateFormat timeFormat = new SimpleDateFormat("h.mma", Locale.ENGLISH);

	/**
	 * @param milliseconds
	 * @see AbstractTimeValue
	 */
	AbstractTime(final long milliseconds)
	{
		super(milliseconds);
	}

	/**
	 * Returns true if this Time value is after the given
	 * Time argument's value.
	 * 
	 * @param that
	 *            the AbstractTimeValue to compare with
	 * @return true if this Time value is after that
	 *         Time value
	 */
	public final boolean after(final AbstractTimeValue that)
	{
		return greaterThan(that);
	}

	/**
	 * Returns true if this Time value is before the given
	 * Time argument's value.
	 * 
	 * @param that
	 *            the AbstractTimeValue to compare with
	 * @return true if this Time value is before that
	 *         Time value
	 */
	public final boolean before(final AbstractTimeValue that)
	{
		return lessThan(that);
	}

	/**
	 * Converts this Time to a time String using the formatter 'h.mma'.
	 * 
	 * @return the Time String
	 */
	public final String toTimeString()
	{
		return toTimeString(localtime);
	}

	/**
	 * Converts this Time to a Date String using the Date
	 * formatter 'h.mma'.
	 * 
	 * @param calendar
	 *            the Calendar to use in the conversion
	 * @return the Date String
	 */
	public final String toTimeString(final Calendar calendar)
	{
		synchronized (timeFormat)
		{
			synchronized (calendar)
			{
				timeFormat.setCalendar(calendar);
				return timeFormat.format(new Date(getMilliseconds())).toLowerCase();
			}
		}
	}

	/**
	 * Converts this Time to a String suitable for use in a file system
	 * name.
	 * 
	 * @return this Time as a formatted String
	 */
	@Override
	public String toString()
	{
		return toTimeString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy