android将当前日期时间转换为我想要的日期数据类型格式,而不是字符串

问题描述 投票:-2回答:1

我的数据库有DATETIME数据类型,所以我需要将DATE格式传递给我的API参数。我在网上找到的所有内容都是将日期转换为字符串类型,这不是我想要的

android date format date-format
1个回答
0
投票

这样的事情应该有效。您的API显然不是以正确的格式查找实际的DATETIME对象,如下所示:

    public static String getFormattedDateTime(Date date) {
        // Get date string for today to get today's jobs
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(date);
    }

如果您有日期字符串而不是日期对象,则可以使用此类方法。您将在定义dateFormat后解析字符串:

public static Date getDateObjectFromDateTime(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        Date inputDate = null;
        try {
            inputDate = dateFormat.parse(dateString);
        } catch(ParseException e) {
            e.printStackTrace();
        }
        return inputDate;
    }
© www.soinside.com 2019 - 2024. All rights reserved.