Here you can find the source of formatDateWithMilliseconds(DateFormat aFormat, Date aDate)
Parameter | Description |
---|---|
aFormat | the base format |
aDate | the date to format |
public static String formatDateWithMilliseconds(DateFormat aFormat, Date aDate)
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 Rene Schneider, GEBIT Solutions GmbH and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ import java.lang.reflect.Field; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Main { /**/*w w w . ja va 2 s .c o m*/ * Performs date formatting using the provided format, but injects millisecond precision if the millisecond value is * not "000". This is intended to be used with the standard date/time formats returned by DateFormat factory * methods, which don't include milliseconds in most cases unfortunately. It employs a rather ugly hack to enhance * the pattern which only works with {@link SimpleDateFormat}, but as far as I know there's no better method to * achieve this result. * * @param aFormat * the base format * @param aDate * the date to format * @return the formatted string */ public static String formatDateWithMilliseconds(DateFormat aFormat, Date aDate) { DateFormat tempFormat = aFormat; if (aDate.getTime() % 1000 != 0) { if (aFormat instanceof SimpleDateFormat) { Field tempField; try { tempField = SimpleDateFormat.class.getDeclaredField("pattern"); tempField.setAccessible(true); String tempPattern = (String) tempField.get(aFormat); tempPattern = tempPattern.replace(":ss", ":ss.SSS"); tempFormat = new SimpleDateFormat(tempPattern); } catch (SecurityException exc) { exc.printStackTrace(); } catch (NoSuchFieldException exc) { exc.printStackTrace(); } catch (IllegalArgumentException exc) { exc.printStackTrace(); } catch (IllegalAccessException exc) { exc.printStackTrace(); } } } return tempFormat.format(aDate); } }