Here you can find the source of formatISODate(String date)
Parameter | Description |
---|---|
date | The input date to format |
public static String formatISODate(String date)
//package com.java2s; /**// ww w . j ava 2 s . c o m * Archivists' Toolkit(TM) Copyright 2005-2007 Regents of the University of California, New York University, & Five Colleges, Inc. * All rights reserved. * * This software is free. You can redistribute it and / or modify it under the terms of the Educational Community License (ECL) * version 1.0 (http://www.opensource.org/licenses/ecl1.php) * * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ECL license for more details about permissions and limitations. * * * Archivists' Toolkit(TM) * http://www.archiviststoolkit.org * info@archiviststoolkit.org * * * Simple utility class for checking that an entered date is valid for the given database * and for handeling iso dates in the AT * * Created by IntelliJ IDEA. * User: Nathan Stevens * Date: Aug 28, 2008 * Time: 10:46:58 AM * To change this template use File | Settings | File Templates. */ public class Main { /** * Method to format an inputed date to the iso format of either * yyyy, yyyy-mm, or yyyy-mm-dd * @param date The input date to format * @return The formated iso date */ public static String formatISODate(String date) { if (date.matches("\\d{1,4}")) { // matches yyyy return padYear(date); } else if (date.matches("\\d{1,4}-\\d{1,2}")) { // matches yyyy-mm format String[] sa = date.split("-"); return padYear(sa[0]) + "-" + padMonthOrDay(sa[1]); } else if (date.matches("\\d{1,4}-\\d{1,2}-\\d{1,2}")) {// matches yyyy-mm-dd String[] sa = date.split("-"); return padYear(sa[0]) + "-" + padMonthOrDay(sa[1]) + "-" + padMonthOrDay(sa[2]); } else { // return blank return ""; } } /** * Method to pad the given year with leading zero * @param year The year that is pasted in * @return The year paded with zero or an empty string if not a valid year */ public static String padYear(String year) { // fisrt check to see if it is a year that needs to be padded if (year.matches("\\d{4}")) { return year; } else if (year.matches("\\d{3}")) { return "0" + year; } else if (year.matches("\\d{2}")) { return "00" + year; } else if (year.matches("\\d{1}")) { return "000" + year; } else { // just return the year return ""; // year was not good } } /** * Pad the month or day with a leading zero * @param mod The month or day to pad * @return The padded month or day or a blank string if not a valid day or year */ public static String padMonthOrDay(String mod) { // fisrt check to see if it is a year that needs to be padded if (mod.matches("\\d{2}")) { return mod; } else if (mod.matches("\\d")) { return "0" + mod; } else { // month or day format not right return blank return ""; } } }