質問

I wish to replace the following function with a better less detailed process for determining the numeric month value from a String. Does anyone know if there is a Date/DateTime function which can be used for this? I did not note one which would meet my needs.

The text strings in the code are exactly how I receive them.

Thanks in advance for any help.

private static Integer convertMonthTextToNumber(String matrixMonth){
    if(matrixMonth == 'January'){
        return 1;   
    }else if(matrixMonth == 'February'){
        return 2;   
    }else if(matrixMonth == 'March'){
        return 3;   
    }else if(matrixMonth == 'April'){
        return 4;   
    }else if(matrixMonth == 'May'){
        return 5;       
    }else if(matrixMonth == 'June'){            
        return 6;   
    }else if(matrixMonth == 'July'){
        return 7;   
    }else if(matrixMonth == 'August'){
        return 8;   
    }else if(matrixMonth == 'September'){
        return 9;   
    }else if(matrixMonth == 'October'){
        return 10;  
    }else if(matrixMonth == 'November'){
        return 11;  
    }else{
        return 12;
    }
}
役に立ちましたか?

解決

How about this:

private static Map<String, Integer> monthsMap = new Map<String, Integer>{
  "January"  => 1,
  "February" => 2,
  etc.
};

private static Integer convertMonthTextToNumber(String month)
{
  return monthsMap.get( month );
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top