parseposition java_Java 使用 ParsePosition 类的另外一个例子

1.[代码]DateParse2.java

/**

* Copyright (c) Ian F. Darwin, 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

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);

}

//-

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值