문제

I try to get the short name of the month in groovy, but in German... (Jan, Feb, Mrz, Apr...)

I can get it in English with the simple statement:

mydate='2012-11-23 02:26:55.983'
origDate=new Date().parse('yyyy-MM-dd H:mm:ss.S',mydate).format('MMM')

but in german ?

thanks in advance!

도움이 되었습니까?

해결책

Use SimpleDateFormat:

import java.text.SimpleDateFormat

String mydate = '2012-03-23 02:26:55.983'

SimpleDateFormat sdf = new SimpleDateFormat( 'MMM', Locale.GERMANY )

assert sdf.format( Date.parse( 'yyyy-MM-dd H:mm:ss.S', mydate ) ) == 'Mrz'

Longer example:

def months = new SimpleDateFormat( 'MMM', Locale.GERMANY ).with { sdf ->
  (1..12).collect { month ->
    sdf.format( Date.parse( 'MM', "$month" ) )
  }
}
println months

prints:

[Jan, Feb, Mrz, Apr, Mai, Jun, Jul, Aug, Sep, Okt, Nov, Dez]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top