gpt4 book ai didi

Java类给出日期的长字符串格式? (用言语来说,就像说话一样)

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:49 25 4
gpt4 key购买 nike

浏览完日历和日期类后,似乎没有传统的方法可以将时间转换成其单词替代形式。例如,如果时间是中午 12:20,那么我希望将其转换为下午十二点四十五分。

是否有任何预制类(class)?日期类中的一半方法已被弃用。

最佳答案

虽然没有类可以满足您的要求,但我确实有一个与此类似的示例:

/**
* This class will take an integer value, positive or negative, and translate
* it into printed single digit values.
* i.e. 142 = one four two
* -12 = negative one two
*/

public class NumberTranslator
{

final private static String[] txt = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
static int tempNumber;
static String finalString;
static int tempNumber1;
public static StringBuilder num = new StringBuilder();

public static void main(String[] args)
{
if(args.length == 0)
{
System.out.println("You need to pass a number as an argument");
return;
}
NumberTranslator nt = new NumberTranslator();
System.out.println(nt.translate(args[0]));
}

public static String translate(int number) {
return translate(String.valueOf(number));
}

public static String translate(String number) {
if(!isNumeric(number))
return "";
boolean isNegative = false;
StringBuilder num = new StringBuilder();
if(number.charAt(0) == '-')
{
num.append("negative ");
isNegative = true;
}
if(isNegative)
{
number = number.substring(1,number.length());
for (Character c : number.toCharArray()) {
num.append(txt[Integer.valueOf(c.toString())]).append(" ");
}
}
else
{
for (Character c : number.toCharArray()) {
num.append(txt[Integer.valueOf(c.toString())]).append(" ");
}
}
return num.toString().trim();
}

private static boolean isNumeric(String value)
{
//Ignore the negative character
if(value.charAt(0) == '-')
{
for(int i = 1; i < value.length(); i++)
{
if(Character.isDigit(value.charAt(i)) == false)
return false;
}
}
else
{
for(int i = 0; i < value.length(); i++)
{
if(Character.isDigit(value.charAt(i)) == false)
return false;
}
}
return true;
}

}

关于Java类给出日期的长字符串格式? (用言语来说,就像说话一样),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com