如何在groovy中将24小时格式转换为12小时格式

如何在groovy中将24小时格式转换为12小时格式,groovy,Groovy,我是groovy新手,我想将24小时格式转换为12小时格式。用于它的代码是什么?有内置的方法吗 我只想要groovy代码而不是java one我认为这个问题有点类似于。只是Java和Groovy有很多相似之处。为了指出这一点,我从前面提到的问题中得到了Cloud的答案,并将其转换为Groovy import java.time.LocalTime import java.time.format.DateTimeFormatter final String time = "21:49" Str

我是groovy新手,我想将24小时格式转换为12小时格式。用于它的代码是什么?有内置的方法吗


我只想要groovy代码而不是java one

我认为这个问题有点类似于。只是Java和Groovy有很多相似之处。为了指出这一点,我从前面提到的问题中得到了Cloud的答案,并将其转换为Groovy

import java.time.LocalTime
import java.time.format.DateTimeFormatter

final String time = "21:49"

String result = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm")).format(DateTimeFormatter.ofPattern("hh:mm a"));

println(result)
如果您想构建自己的时间函数,可以尝试自定义下面的代码

final String time = "21:49"

final String _24to12( final String input ){
    if ( input.indexOf(":") == -1 )  
        throw ("")

    final String []temp = input.split(":")

    if ( temp.size() != 2 )
        throw ("")  // Add your throw code
                    // This does not support time string with seconds

    int h = temp[0] as int  // if h or m is not a number then exception
    int m = temp[1] as int  // java.lang.NumberFormatException will be raised
                            // that can be cached or just terminate the program
    String dn

    if ( h < 0 || h > 23 )
        throw("")  // add your own throw code
                   // hour can't be less than 0 or larger than 24

    if ( m < 0 || m > 59 )
        throw("")  // add your own throw code 
                   // minutes can't be less than 0 or larger than 60

    if ( h == 0 ){
        h = 12
        dn = "AM"
    } else if ( h == 12 ) {
        dn = "PM"
    } else if ( h > 12 ) {
        h = h - 12
        dn = "PM"
    } else {
        dn = "AM"
    }

    return h.toString() + ":" + m.toString() + " " + dn.toString()
}

println(_24to12(time))
final String time=“21:49”
最终字符串_24to12(最终字符串输入){
if(input.indexOf(“:”)==-1)
掷(“”)
最终字符串[]temp=input.split(“:”)
如果(温度大小()!=2)
throw(“”//添加您的throw代码
//这不支持带秒的时间字符串
int h=temp[0]为int//如果h或m不是数字,则为异常
将引发int//java.lang.NumberFormatException时的int m=temp[1]
//可以缓存或只是终止程序
字符串dn
if(h<0 | | h>23)
throw(“”//添加您自己的throw代码
//小时不能小于0或大于24
如果(m<0 | | m>59)
throw(“”//添加您自己的throw代码
//分钟数不能小于0或大于60
如果(h==0){
h=12
dn=“AM”
}else如果(h==12){
dn=“PM”
}否则,如果(h>12){
h=h-12
dn=“PM”
}否则{
dn=“AM”
}
返回h.toString()+”:“+m.toString()+”+dn.toString()
}
println(_24to12(时间))

我认为这个问题与。只是Java和Groovy有很多相似之处。为了指出这一点,我从前面提到的问题中得到了Cloud的答案,并将其转换为Groovy

import java.time.LocalTime
import java.time.format.DateTimeFormatter

final String time = "21:49"

String result = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm")).format(DateTimeFormatter.ofPattern("hh:mm a"));

println(result)
如果您想构建自己的时间函数,可以尝试自定义下面的代码

final String time = "21:49"

final String _24to12( final String input ){
    if ( input.indexOf(":") == -1 )  
        throw ("")

    final String []temp = input.split(":")

    if ( temp.size() != 2 )
        throw ("")  // Add your throw code
                    // This does not support time string with seconds

    int h = temp[0] as int  // if h or m is not a number then exception
    int m = temp[1] as int  // java.lang.NumberFormatException will be raised
                            // that can be cached or just terminate the program
    String dn

    if ( h < 0 || h > 23 )
        throw("")  // add your own throw code
                   // hour can't be less than 0 or larger than 24

    if ( m < 0 || m > 59 )
        throw("")  // add your own throw code 
                   // minutes can't be less than 0 or larger than 60

    if ( h == 0 ){
        h = 12
        dn = "AM"
    } else if ( h == 12 ) {
        dn = "PM"
    } else if ( h > 12 ) {
        h = h - 12
        dn = "PM"
    } else {
        dn = "AM"
    }

    return h.toString() + ":" + m.toString() + " " + dn.toString()
}

println(_24to12(time))
final String time=“21:49”
最终字符串_24to12(最终字符串输入){
if(input.indexOf(“:”)==-1)
掷(“”)
最终字符串[]temp=input.split(“:”)
如果(温度大小()!=2)
throw(“”//添加您的throw代码
//这不支持带秒的时间字符串
int h=temp[0]为int//如果h或m不是数字,则为异常
将引发int//java.lang.NumberFormatException时的int m=temp[1]
//可以缓存或只是终止程序
字符串dn
if(h<0 | | h>23)
throw(“”//添加您自己的throw代码
//小时不能小于0或大于24
如果(m<0 | | m>59)
throw(“”//添加您自己的throw代码
//分钟数不能小于0或大于60
如果(h==0){
h=12
dn=“AM”
}else如果(h==12){
dn=“PM”
}否则,如果(h>12){
h=h-12
dn=“PM”
}否则{
dn=“AM”
}
返回h.toString()+”:“+m.toString()+”+dn.toString()
}
println(_24to12(时间))

凯文的答案是正确的,应该会得到答案。。。我只发布了这个,因为它稍微短一点

import java.time.*
import static java.time.format.DateTimeFormatter.ofPattern

String time = '13:23:45'

String result = LocalTime.parse(time).format(ofPattern('h:mm:ss a'))
println result

凯文的答案是正确的,应该会被扣分。。。我只发布了这个,因为它稍微短一点

import java.time.*
import static java.time.format.DateTimeFormatter.ofPattern

String time = '13:23:45'

String result = LocalTime.parse(time).format(ofPattern('h:mm:ss a'))
println result

24小时格式。。。在什么数据结构中?请在问题中发布相关代码。24小时格式。。。在什么数据结构中?请在问题中张贴相关代码。