如何将所有月份名称放在hashmap中,并将1月份替换为1月份和2月份?

时间:2014-02-25 06:18:44

标签: java hashmap

HashMap hm = new HashMap();
        hm.put("January", "Jan");
        hm.put("Febraury", "Feb");
        hm.put("March", "Mar");
        hm.put("April", "Apr");
        hm.put("May", "May");
        hm.put("June", "Jun");
        hm.put("July", "Jul");
        hm.put("August", "Aug");
        hm.put("September", "Sep");
        hm.put("October", "Oct");
        hm.put("November", "Nov");
        hm.put("December", "Dec");
        for (int k = 1; k <= 12; k++) {
            if (Time.contains("January")) {

            }
        }

如果Time包含2013年9月25日,那么预期产量是2013年9月25日。我可以这样做,感谢提前

5 个答案:

答案 0 :(得分:3)

使用simpleDateFormat将日期转换为所需的模式。这会将日期转换为您想要的格式,您无需将Janurary保留为hashmap中的Jan。

String date = "September 25,2013";
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy");
System.out.println(formatter.format(new Date(date)));

输出:

Sep 25,2013

答案 1 :(得分:1)

试试以下内容。

 public static void main(String[] args) throws Exception {

            String time = "September 25,2013";
            HashMap<String,String> hm = new HashMap<String, String>();
            hm.put("January", "Jan");
            hm.put("Febraury", "Feb");
            hm.put("March", "Mar");
            hm.put("April", "Apr");
            hm.put("May", "May");
            hm.put("June", "Jun");
            hm.put("July", "Jul");
            hm.put("August", "Aug");
            hm.put("September", "Sep");
            hm.put("October", "Oct");
            hm.put("November", "Nov");
            hm.put("December", "Dec");
            for (String key : hm.keySet()) {
                if (time.contains(key)) {
                    String newTime = time.replace(key, hm.get(key));
                    System.out.println(newTime);
                }
            }
        }

答案 2 :(得分:0)

如果time是:

String time = "September 25, 2013";

然后您可以使用String#replace()方法:

time = time.replace("September", (String) hm.get("September"));

注意:为了避免明确强制转换String,您可以声明HashMap之类的内容:

HashMap<String, String> hm = new HashMap<>();

如果你想遍历EntrySet的{​​{1}},所以你要替换键的每一个,请使用HashMap循环:

for

答案 3 :(得分:0)

这可能对您有所帮助

    HashMap hm = new HashMap();
    hm.put("January", "Jan");
    hm.put("Febraury", "Feb");
    hm.put("March", "Mar");
    hm.put("April", "Apr");
    hm.put("May", "May");
    hm.put("June", "Jun");
    hm.put("July", "Jul");
    hm.put("August", "Aug");
    hm.put("September", "Sep");
    hm.put("October", "Oct");
    hm.put("November", "Nov");
    hm.put("December", "Dec");

    String time = "September 25,2013";
    Set hset = hm.keySet();
   Iterator ite =  hset.iterator();    

    while(ite.hasNext()) {          
        String monthName = (String)ite.next();
         if (time.contains(monthName)) {            
             time = time.replace(monthName, (String)hm.get(monthName));
             break;

         }
    }
    System.out.println(time);

答案 4 :(得分:0)

java.time在Java 8中

如果您的问题实际上是关于如何格式化日期(如您在上一行中所述),而不是真正关于HashMap,那么这里是如何使用java.time package中的新Java 8来完成的。 java.time包的灵感来自Joda-Time,由JSR 310定义。

请注意,您可以轻松地将各种语言及其各种习惯本地化,例如月份名称缩写中的不同字符数。我正在显示法国的格式作为输出,但您可以切换到Locale.US

String input = "September 25, 2013";
DateTimeFormatter formatterInput = DateTimeFormatter.ofPattern( "MMMM d, yyyy" ).withLocale( java.util.Locale.ENGLISH );
LocalDate localDate = LocalDate.parse( input, formatterInput );

//DateTimeFormatter formatterOutput = DateTimeFormatter.ofPattern( "MMM" ).withLocale( Locale.CANADA_FRENCH );
DateTimeFormatter formatterOutput = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( Locale.FRANCE );

String output = formatterOutput.format( localDate );

转储到控制台...

System.out.println( "localDate: " + localDate );
System.out.println( "output: " + output ););

跑步时......

localDate: 2013-09-25
output: 25 sept. 2013