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

de.westnordost.osmapi.traces.GpxDateFormat Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
package de.westnordost.osmapi.traces;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Pattern;

import de.westnordost.osmapi.common.OsmXmlDateFormat;

/** Gpx timestamps can optionally include milliseconds */
public class GpxDateFormat extends OsmXmlDateFormat
{
	private static final SimpleDateFormat MILLIS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
	static {
		MILLIS.setTimeZone(TimeZone.getTimeZone("UTC"));
	}

	private static final Pattern MILLIS_PATTERN = Pattern.compile("\\.[0-9]{3}");
	private static boolean hasMillis(String source)
	{
		return MILLIS_PATTERN.matcher(source).find();
	}
	
	public Date parse(String source) throws ParseException
	{
		// optional: parse milliseconds
		if(hasMillis(source))
		{
			return MILLIS.parse(source);
		}
		return super.parse(source);
	}
	
	public String format(Date date)
	{
		if(date.getTime() % 1000 > 0)
		{
			return MILLIS.format(date);
		}
		return super.format(date);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy