一个java笔试题的实现:用5位字符表示日期,并且要求500年不能重复

93 篇文章 0 订阅

package com.zhaosoft.bean;

public class Nyr {

 /***************************************************************************
  * 输入一个日期,返回5位字符串
  *
  * @param rq
  *            日期(2007-12-28) 要求从1000-1999年
  * @author zhaosoft
  * @date 2007-12-28
  **************************************************************************/
 public String get5Nyr(String rq) {

  String myYear = "";
  String myMonth = "";
  String myDate = "";
  String StrSc = "";
  if ((rq != null) && (!rq.equals(""))) {
   int year = Integer.parseInt(rq.substring(0, 4));
   if (year >= 1000 && year <= 1999) {
    myYear = rq.substring(1, 4);
    myMonth = rq.substring(5, 7);
    myDate = rq.substring(8);
    if (myMonth.equals("10")) {
     myMonth = "a";
    } else if (myMonth.equals("11")) {
     myMonth = "b";
    } else if (myMonth.equals("12")) {
     myMonth = "c";
    } else {
     myMonth = "" + Integer.parseInt(myMonth);
    }

    if (Integer.parseInt(myDate) > 9) {

     switch (Integer.parseInt(myDate)) {

      case 10 :
       myDate = "a";
       break;
      case 11 :
       myDate = "b";
       break;
      case 12 :
       myDate = "c";
       break;
      case 13 :
       myDate = "d";
       break;
      case 14 :
       myDate = "e";
       break;
      case 15 :
       myDate = "f";
       break;
      case 16 :
       myDate = "g";
       break;
      case 17 :
       myDate = "h";
       break;
      case 18 :
       myDate = "i";
       break;
      case 19 :
       myDate = "j";
       break;
      case 20 :
       myDate = "k";
       break;
      case 21 :
       myDate = "l";
       break;
      case 22 :
       myDate = "m";
       break;
      case 23 :
       myDate = "n";
       break;
      case 24 :
       myDate = "o";
       break;
      case 25 :
       myDate = "p";
       break;
      case 26 :
       myDate = "q";
       break;
      case 27 :
       myDate = "r";
       break;
      case 28 :
       myDate = "s";
       break;
      case 29 :
       myDate = "t";
       break;
      case 30 :
       myDate = "u";
       break;
      case 31 :
       myDate = "v";
       break;
     }
    } else {
     myDate = "" + Integer.parseInt(myDate);
    }
    StrSc = myYear + myMonth + myDate;
   } else {
    StrSc = "您输入的日期超出范围!";
    // System.out.println("您输入的日期超出范围!");

   }
  }
  return StrSc;

 }
 /***************************************************************************
  * 由5位的年月日返回与之相对的日期
  * @param  5位字符表示的日期 日期(007ab)
  * @author zhaosoft
  * @date   2007-12-28
  * @return 返回的日期  要求从1000-1999年
  *
  **************************************************************************/
 public String getDateFrom5NYR(String nyr) {
  String myYear="";
  String myMonth="";
  String myDate="";
  
  myYear="1"+nyr.substring(0,3);
  
  if(nyr.substring(3,4).equals("a")){
   myMonth="10";
  }else if(nyr.substring(3,4).equals("b")){
   myMonth="11";
  }else if(nyr.substring(3,4).equals("c")){
   myMonth="12";
  }else{
   myMonth="0"+nyr.substring(3,4);
  }
  
  if(nyr.substring(4).equals("a")){
   myDate="10";
  }else if(nyr.substring(4).equals("b")){
   myDate="11";
  }else if(nyr.substring(4).equals("c")){
   myDate="12";
  }else if(nyr.substring(4).equals("d")){
   myDate="13";
  }else if(nyr.substring(4).equals("e")){
   myDate="14";
  }else if(nyr.substring(4).equals("f")){
   myDate="15";
  }else if(nyr.substring(4).equals("g")){
   myDate="16";
  }else if(nyr.substring(4).equals("h")){
   myDate="17";
  }else if(nyr.substring(4).equals("i")){
   myDate="18";
  }else if(nyr.substring(4).equals("j")){
   myDate="19";
  }else if(nyr.substring(4).equals("k")){
   myDate="20";
  }else if(nyr.substring(4).equals("l")){
   myDate="21";
  }else if(nyr.substring(4).equals("m")){
   myDate="22";
  }else if(nyr.substring(4).equals("n")){
   myDate="23";
  }else if(nyr.substring(4).equals("o")){
   myDate="24";
  }else if(nyr.substring(4).equals("p")){
   myDate="25";
  }else if(nyr.substring(4).equals("q")){
   myDate="26";
  }else if(nyr.substring(4).equals("r")){
   myDate="27";
  }else if(nyr.substring(4).equals("s")){
   myDate="28";
  }else if(nyr.substring(4).equals("t")){
   myDate="29";
  }else if(nyr.substring(4).equals("u")){
   myDate="30";
  }else if(nyr.substring(4).equals("v")){
   myDate="31";
  }else{
   myDate="0"+nyr.substring(4);
  }
  return myYear+"-"+myMonth+"-"+myDate;
 }
 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {

  String a=new Nyr().get5Nyr("1999-12-20");
  System.out.println(a);
  System.out.println(new Nyr().getDateFrom5NYR(a));

 }

}
  

  我喜欢的
浏览器
  我喜欢的
文化礼品
  我喜欢的
ISP网站
  我喜欢的
网站
FireFox 2.0
100部奥斯卡影片
时代互联
博告网
   
   
  时代互联10元换空间
  加入博告网日进斗金不是梦!
聚合到我的好诶网博告网 提供的广告

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值