Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在hashmap中放置所有月份名称,并将一月替换为一月,将二月替换为二月? HashMap hm=newhashmap(); hm.put(“一月”、“一月”); hm.put(“二月”、“二月”); hm.put(“三月”、“三月”); hm.put(“四月”、“四月”); hm.put(“五月”、“五月”); hm.put(“六月”、“六月”); hm.put(“七月”、“七月”); hm.put(“八月”、“八月”); hm.put(“九月”、“九月”); hm.put(“十月”、“十月”); hm.put(“11月”、“11月”); hm.put(“十二月”、“十二月”); 对于(int k=1;k_Java_Hashmap - Fatal编程技术网

Java 如何在hashmap中放置所有月份名称,并将一月替换为一月,将二月替换为二月? HashMap hm=newhashmap(); hm.put(“一月”、“一月”); hm.put(“二月”、“二月”); hm.put(“三月”、“三月”); hm.put(“四月”、“四月”); hm.put(“五月”、“五月”); hm.put(“六月”、“六月”); hm.put(“七月”、“七月”); hm.put(“八月”、“八月”); hm.put(“九月”、“九月”); hm.put(“十月”、“十月”); hm.put(“11月”、“11月”); hm.put(“十二月”、“十二月”); 对于(int k=1;k

Java 如何在hashmap中放置所有月份名称,并将一月替换为一月,将二月替换为二月? HashMap hm=newhashmap(); hm.put(“一月”、“一月”); hm.put(“二月”、“二月”); hm.put(“三月”、“三月”); hm.put(“四月”、“四月”); hm.put(“五月”、“五月”); hm.put(“六月”、“六月”); hm.put(“七月”、“七月”); hm.put(“八月”、“八月”); hm.put(“九月”、“九月”); hm.put(“十月”、“十月”); hm.put(“11月”、“11月”); hm.put(“十二月”、“十二月”); 对于(int k=1;k,java,hashmap,Java,Hashmap,Iftimeis: 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("Ju

If
time
is:

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")) {

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

String time = "September 25, 2013";
注意:为了避免显式转换为
字符串
,您可以声明您的
HashMap
如下:

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

如果
时间
为:

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")) {

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

String time = "September 25, 2013";
注意:为了避免显式转换为
字符串
,您可以声明您的
HashMap
如下:

time = time.replace("September", (String) hm.get("September"));
试试下面的

for (Entry<String, String> es : hm.entrySet()) {
    time = time.replace(es.getKey(), es.getValue());
}
publicstaticvoidmain(字符串[]args)引发异常{
字符串时间=“2013年9月25日”;
HashMap hm=新的HashMap();
hm.put(“一月”、“一月”);
hm.put(“二月”、“二月”);
hm.put(“三月”、“三月”);
hm.put(“四月”、“四月”);
hm.put(“五月”、“五月”);
hm.put(“六月”、“六月”);
hm.put(“七月”、“七月”);
hm.put(“八月”、“八月”);
hm.put(“九月”、“九月”);
hm.put(“十月”、“十月”);
hm.put(“11月”、“11月”);
hm.put(“十二月”、“十二月”);
for(字符串键:hm.keySet()){
if(time.contains(key)){
字符串newTime=time.replace(key,hm.get(key));
系统输出打印项次(新时间);
}
}
}
尝试以下操作

for (Entry<String, String> es : hm.entrySet()) {
    time = time.replace(es.getKey(), es.getValue());
}
publicstaticvoidmain(字符串[]args)引发异常{
字符串时间=“2013年9月25日”;
HashMap hm=新的HashMap();
hm.put(“一月”、“一月”);
hm.put(“二月”、“二月”);
hm.put(“三月”、“三月”);
hm.put(“四月”、“四月”);
hm.put(“五月”、“五月”);
hm.put(“六月”、“六月”);
hm.put(“七月”、“七月”);
hm.put(“八月”、“八月”);
hm.put(“九月”、“九月”);
hm.put(“十月”、“十月”);
hm.put(“11月”、“11月”);
hm.put(“十二月”、“十二月”);
for(字符串键:hm.keySet()){
if(time.contains(key)){
字符串newTime=time.replace(key,hm.get(key));
系统输出打印项次(新时间);
}
}
}

使用SimpleDataFormat将日期转换为所需的模式。这将将日期转换为所需的格式,并且您无需在hashmap中将一月作为一月保留

 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);
                }
            }
        }
输出:

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

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

 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);
                }
            }
        }
输出:

String date = "September 25,2013";
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy");
System.out.println(formatter.format(new Date(date)));
这可能对你有帮助

Sep 25,2013
这可能对你有帮助

Sep 25,2013
Java8中的java.time 如果您的问题实际上是关于如何格式化日期(如您在最后一行中所述),而不是关于HashMap,那么下面介绍如何使用新的in.java.time包来实现它

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

    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);
转储到控制台

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 );
当运行时

localDate:2013-09-25
产出:2013年9月25日
java 8中的java.time 如果您的问题实际上是关于如何格式化日期(如您在最后一行中所述),而不是关于HashMap,那么下面介绍如何使用新的in.java.time包来实现它

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

    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);
转储到控制台

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 );
当运行时

localDate:2013-09-25
产出:2013年9月25日

时间意味着什么?它是一门课吗..你可以使用SimpleDateFormatSpose Time=2013年9月25日你的问题的意图是时间可以是更长的字符串吗?我想到的是“弗雷德里克·马奇于2013年9月25日去世;他的妻子阿普里于第二天去世”.为什么你需要一个hashmap呢?你可以使用SimpleDateFormat来格式化日期字符串时间的含义是什么?它是一个类吗..你可以使用SimpleDateFormatSuperse Time=2013年9月25日你的问题的目的是时间可以是一个更长的字符串吗?我想到的是“弗雷德里克·马奇于2013年9月25日去世;他的妻子在第二天去世”。为什么你需要一个hashmap呢?你可以使用SimpleDataFormat来格式化日期字符串。我怎样才能与我当前的代码集成,你能指导我吗?我会遍历条目集,而不是键集,这样你就不必再次访问映射来获得值。@DavidWallace你是对的,编辑过。user3297398你当前的状态是什么代码?我如何与我当前的代码集成,请您指导我好吗?我将遍历条目集,而不是键集,这样您就不必再次访问映射来获取值。@DavidWallace您是对的,已编辑。user3297398您当前的代码是什么?