【问题标题】:Get first element of ResultTable[] in java在java中获取ResultTable[]的第一个元素
【发布时间】:2019-02-10 02:33:16
【问题描述】:

这些代码:

String genType = FileConstants.CHANGE_CYCLE;
ResultTable[] genericCode = RTManager.getRTCsmGenericCodesDecodeList(genType);
String genCode = Arrays.toString(genericCode);

返回这些值:

genCode = [[code=22:00:00]

[dCode=Cut-off time for change bill_cycle if existing cycle_close_date=activity_date]]

问题:如何只获取'22:00:00' 并将其转换为Time datatype?

【问题讨论】:

  • 第一步是避免使用Arrays.toString,而是使用genericCode[0].toString()。请告诉我们返回的内容,以便我们在下一步为您提供最佳帮助。
  • 是什么让您认为您想要Time 类型的结果?该课程早已过时,而且设计总是很糟糕。使用来自java.time, the modern Java date and time API 的LocalTime 可能会更好。

标签: java arrays time


【解决方案1】:

如果 ResultTable 数组中的项目有一个 toString() 并且它产生字符串,那么你可以像这样得到它。

genericCode[0].toString().split("=")[1]


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {

    public static void main(String[] args) {

        String time = "22:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        Date date;
        try {
            date = sdf.parse(time);
            System.out.println("Time: " + sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

【讨论】:

  • 从这里,如何将其转换为格式为DateFormat formatTime = new SimpleDateFormat("HH:mm:ss"); 的时间?
  • 我收到错误:unreported exception java.text.ParseException; must be caught or declared to be thrown for Date date = sdf.parse(time);
  • 打印日期怎么会变成:Thu Jan 01 22:00:00 IST 1970?虽然sdf 只是HH:mm:ss
  • 不要直接打印日期。按照我在代码中显示的方式打印。 sdf.format(日期)
  • 请不要教年轻人使用早已过时且臭名昭著的麻烦SimpleDateFormat类。至少不是第一选择。而且不是没有任何保留。今天我们在java.time, the modern Java date and time API 和它的DateTimeFormatter 中做得更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
相关资源
最近更新 更多