Here you can find the source of parse1(String DateString, String dateTimeSeparator)
Parameter | Description |
---|---|
DateString | a String that represents a data |
private static Date parse1(String DateString, String dateTimeSeparator)
//package com.java2s; /*/*from ww w .j a va 2s.co m*/ * File: NxNodeUtils.java * * Copyright (C) 2001, Ruth Mikkelson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * Contact : Ruth Mikkelson <mikkelsonr@uwstout.edu> * Department of Mathematics, Statistics and Computer Science * University of Wisconsin-Stout * Menomonie, WI 54751, USA * * This work was supported by the Intense Pulsed Neutron Source Division * of Argonne National Laboratory, Argonne, IL 60439-4845, USA. * * For further information, see <http://www.pns.anl.gov/ISAW/> * * Modified: * * $Log$ * Revision 1.25 2007/08/26 23:55:44 rmikk * Changed package name for the NeXus package * * Revision 1.24 2005/02/03 08:18:07 kramer * * Fixed an error in the getISO8601String(Date date) method, the current date * was used instead of 'date'. * * Commented out the section that writes fractions of a second because * parseISO8601(String dateString) can't read a ISO8601 string written to a * fraction of a second. * * Revision 1.23 2005/02/03 06:36:50 kramer * * Added the parseISO8601(String dateString) and getISO8601String(Date date) * methods which get a Date from an String written in ISO8601 format and a * String written in ISO8601 format from a Date. * * Revision 1.22 2004/12/23 18:47:05 rmikk * Fixed indentation and added lines between code. * * Revision 1.21 2004/05/14 15:03:27 rmikk * Removed unused variables * * Revision 1.20 2004/03/15 03:36:01 dennis * Moved view components, math and utils to new source tree * gov.anl.ipns.* * * Revision 1.19 2004/02/16 02:15:55 bouzekc * Removed unused import statements. * * Revision 1.18 2003/10/19 20:01:57 rmikk * Added some documentation * Fixed new units for time to be us * Fixed error in calculating factor with a numeric prefix * * Revision 1.17 2003/10/15 03:05:44 bouzekc * Fixed javadoc errors. * * Revision 1.16 2003/09/14 16:35:34 rmikk * -incorporated leading factors like 100us for times and * lengths * -Finished incorporating the time units up to hours. * * Revision 1.15 2003/06/18 20:32:54 pfpeterson * Removed deprecated method. * * Revision 1.14 2003/06/18 19:38:55 pfpeterson * ShowW() now calls method in StringUtil. * * Revision 1.13 2002/11/27 23:28:17 pfpeterson * standardized header * * Revision 1.12 2002/11/20 16:14:50 pfpeterson * reformating * * Revision 1.11 2002/06/19 16:27:04 rmikk * Eliminated commented out code. * Eliminated reference to deprecated Date.getYear() * Fixed code alignment and spacing * * Revision 1.10 2002/06/19 15:55:22 rmikk * Fixed the order of Date formats so that the seconds, when * there, is found * * Revision 1.9 2002/04/01 20:45:41 rmikk * Fixed Date Format exception report in jdk1.4 * Added some support for the Nexus NXChar type * * Revision 1.8 2002/02/26 15:46:41 rmikk * Fixed the utility Showw routine * Added a utility routine to getConversion factors * */ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; public class Main { static String[] Months = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; /** * Attempts to parse a date string with various formats including * "- ,/, and ." separators for month,year,day specifiers. * @param DateString a String that represents a data * @return The Date object corresponding to the DateString */ private static Date parse1(String DateString, String dateTimeSeparator) { Date Result; SimpleDateFormat fmt = new SimpleDateFormat(); fmt.setLenient(true); //yyyy matches with yy String Date_formats[] = { "yyyy", "yyyy-MM", "yyyy-MM-dd", "yyyy.MM.dd", "yy-MMM-dd",//"yyyy-MMM-dd" , "yyyy.MMM.dd" , "yyyy-M-d", "yyyy.M.d", "yy.MM.dd", "yy.N.d", "MM/dd/yyyy", "MM/dd/yy", "M/d/yyyy", "M/d/yy", "MMM/dd/yyyy", "MMM/d/yyyy", "MMM/dd/yy", "MMM/d/yy", "MMM dd/yyyy", "MMM d/yyyy", "MMM dd/yy", "MMM d/yy", "dd-MMM-yyyy", "dd/MMM/yyyy", "dd-MMM-yy", "d/MMM/yy", "MMM dd,yy" }; String Time_formats[] = { "HH:mm:ss", "HH:mm", "hh:mm a", "hh:mma", "H:mm:ss", "H:mm", "h:mm a", "h:mma", "H:m:ss", "H:m", "h:m a", "h:ma", "" }; for (int i = 0; i < Date_formats.length; i++) { for (int k = 0; k < Time_formats.length; k++) { String pattern = Date_formats[i] + dateTimeSeparator + Time_formats[k]; pattern = pattern.trim(); try { fmt.applyPattern(pattern); Result = fmt.parse(DateString); if (Result != null) { return Result; } } catch (Exception s) {// let it drop on the floor } } }//for i return null; } /** * Attempts to parse a date string with various formats including * "- ,/, and ." separators for month,year,day specifiers. * @param DateString a String that represents a data * @return The Date object corresponding to the DateString */ public static Date parse(String DateString) { return parse1(DateString, " "); } /** * Attempts to parse a date string with various formats including * "- ,/, and ." separators for month,year,day specifiers. * @param DateString a String that represents a data * @return The Date object corresponding to the DateString */ private static Date parse(String DateString, char dateTimeSeparator) { if (DateString == null || DateString.length() < 1) return null; int k = DateString.indexOf(dateTimeSeparator); if (k < 0) k = DateString.length(); String[] Splits = SplitUpString(DateString.substring(0, k), " ,/-"); if (Splits == null || Splits.length < 1) return null; int monthIndx, yearIndx, dayIndx; monthIndx = yearIndx = dayIndx = -1; if (!Character.isDigit(Splits[0].charAt(0))) monthIndx = 0; else if (Splits.length > 2 && !Character.isDigit(Splits[2].charAt(0))) monthIndx = 2; if (monthIndx != 0) yearIndx = 0; else if (monthIndx == 0) yearIndx = Splits.length - 1; if (yearIndx < 0) if (Splits[0].length() >= 3 && monthIndx != 0) yearIndx = 0; else if (monthIndx != Splits.length - 1 && Splits[Splits.length - 1].length() >= 3) yearIndx = Splits.length - 1; if (Splits.length < 2) yearIndx = 0; //defaults for yearIndx for 2 digit yearsif no Jan if (yearIndx < 0) if ("/ ".indexOf(Splits[1].charAt(0)) >= 0) yearIndx = Splits.length - 1; else yearIndx = 0; if (yearIndx == 0) if (monthIndx == 2) dayIndx = 4; else if (monthIndx == 4) dayIndx = 2; else if (Splits.length >= 2) if (Splits[1].equals("/") || Splits[1].equals(" ")) { monthIndx = 2; dayIndx = 4; } else { monthIndx = 4; dayIndx = 2; } if (yearIndx > 0) if (monthIndx == 0) { if (yearIndx > 2) dayIndx = 2; } else if (monthIndx > 0) { dayIndx = 0; } else if (Splits.length >= 2) if (Splits[1].equals("/") || Splits[1].equals(" ")) { monthIndx = 0; if (yearIndx > 2) dayIndx = 2; } else if (yearIndx > 2) { dayIndx = 0; monthIndx = 2; } else { monthIndx = 0; } int year, day; int month; try { year = Integer.parseInt(Splits[yearIndx]); } catch (Exception s) { return null; } if (year < 1000) if (year < 30) year += 2000; else year += 1900; year = year - 1900; try { month = Integer.parseInt(Splits[monthIndx]); ; } catch (Exception s1) { month = -1; if (monthIndx >= 0 && monthIndx < Splits.length && Splits[monthIndx].length() >= 3) { String Mnth = Splits[monthIndx].toUpperCase().substring(0, 3); for (int i = 0; i < 11 && month < 0; i++) if (Mnth.equals(Months[i])) month = i + 1; } } month = month - 1; try { day = Integer.parseInt(Splits[dayIndx]); } catch (Exception s2) { day = 1; } //------------------ Now do hours minutes and seconds String SS = DateString.substring(k); if (SS != null) SS = SS.trim(); Splits = SplitUpString(SS, " :"); int hours, minutes; float seconds; hours = minutes = -1; seconds = -1; boolean done = false; try { hours = Integer.parseInt(Splits[0]); } catch (Exception s2) { hours = -1; } if (hours >= 0 && Splits.length > 2 && Splits[1].equals(":")) try { minutes = Integer.parseInt(Splits[2]); } catch (Exception s3) { minutes = -1; } if (minutes >= 0 && Splits.length > 4 && Splits[3].equals(":")) try { seconds = Float.parseFloat(Splits[4]); } catch (Exception s3) { seconds = -1; } int kk = Splits.length - 1; if (hours >= 0 && kk - 1 > 0 && Splits[kk - 1] == " ") if (Splits[kk].toUpperCase().charAt(0) == 'P') hours += 12; if (hours < 0) hours = 0; if (minutes < 0) minutes = 0; if (seconds < 0) seconds = 0; return new Date(year, month, day, hours, minutes, (int) (seconds + .5)); } private static String[] SplitUpString(String toBsplit, String SplitChars) { Vector<String> Res = new Vector<String>(); String CurrentLetters = null; if (toBsplit == null || toBsplit.length() < 1) return null; for (int i = 0; i < toBsplit.length(); i++) { char c = toBsplit.charAt(i); if (SplitChars.indexOf(c) >= 0) { if (CurrentLetters != null) Res.add(CurrentLetters); CurrentLetters = null; if (Res.size() > 0 && c == ' ' && Res.lastElement() == " ") { } else Res.add("" + c); } else { if (CurrentLetters == null) CurrentLetters = "" + c; else CurrentLetters += c; } } if (CurrentLetters != null) Res.add(CurrentLetters); return Res.toArray(new String[0]); } }