- /**
- * Copyright (c) Ian F. Darwin, <a href="http://www.darwinsys.com/">http://www.darwinsys.com/, 1996-2002.
- * All rights reserved. Software written by Ian F. Darwin and others.
- * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
- */
- import java.text.*;
- import java.util.*;
- /** Show some date uses */
- public class DateParse2 {
- public static void main(String[] args) {
- //+
- SimpleDateFormat formatter =
- new SimpleDateFormat ("yyyy-MM-dd");
- String input[] = {
- "BD: 1913-10-01 Vancouver, B.C.",
- "MD: 1948-03-01 Ottawa, ON",
- "DD: 1983-06-06 Toronto, ON" };
- for (int i=0; i<input.length; i++) {
- String aLine = input[i];
- String action;
- switch(aLine.charAt(0)) {
- case 'B': action = "Born"; break;
- case 'M': action = "Married"; break;
- case 'D': action = "Died"; break;
- // others...
- default: System.err.println("Invalid code in " + aLine);
- continue;
- }
- int p = aLine.indexOf(' ');
- ParsePosition pp = new ParsePosition(p);
- Date d = formatter.parse(aLine, pp);
- if (d == null) {
- System.err.println("Invalid date in " + aLine);
- continue;
- }
- String location = aLine.substring(pp.getIndex());
- System.out.println(
- action + " on " + d + " in " + location);
- }
- //-
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/06122012843.html
来源: http://www.codesnippet.cn/detail/06122012843.html