相关疑难解决方法(0)

在Android中将UTC日期转换为当地时间?

我有一个String类似的日期2017-09-16T05:06:18.157,我想将其转换为当地时间 (IST)。在印度标准时间,它将在2017-09-16 10:36:18

使用 Joda-Time,我尝试将其转换为本地,但我无法做到。

下面是我的代码:

private String getConvertDate(String date_server) {
    DateTimeFormatter inputFormatter = DateTimeFormat
            .forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
            .withLocale(Locale.US);

    DateTime parsed = inputFormatter.parseDateTime(date_server);

    DateTimeFormatter outputFormatter = DateTimeFormat
            .forPattern("yyyy-MM-dd HH:mm:ss")
            .withLocale(Locale.US)
            .withZone(DateTimeZone.getDefault());

    return outputFormatter.print(parsed);
}
Run Code Online (Sandbox Code Playgroud)

java timezone datetime android jodatime

0
推荐指数
1
解决办法
7666
查看次数

如何使用SimpleDateFormat解析仅包含月份和年份的日期

我正在处理卡的到期日期.我有一个API,我将以" yyMM "格式将截止日期作为" 字符串 ".我在这里尝试使用

带有TimeZone.getTimeZone的SimpleDateFormat("UTC")

所以我的代码就像

String a= "2011";
SimpleDateFormat formatter = new SimpleDateFormat("yyMM");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(a);
System.out.println(date);
Run Code Online (Sandbox Code Playgroud)

现在问题是,当我通过2011年时,它给出的是 Sat Oct 31 17:00:00 PDT 2020

在这里你可以看到我正在通过11个月,但它将它转换为10月而不是11月.

为什么

还有什么其他选项可以用来将字符串转换yyMM为Date with Timezone?

java datetime date date-parsing simpledateformat

0
推荐指数
2
解决办法
2443
查看次数

从毫秒转换为日期字符串

我尝试从毫秒转换为日期字符串。但是,结果并不像我预期的那样正确。

输入是毫秒(例如:1508206600485

我的时区是 UTC +10:00

------Expected-------------------------------------------- Actual------

01:32 (PM) 17/10/2017--------------------------------02:32 (PM) 17/10/2017
Run Code Online (Sandbox Code Playgroud)

这是方法

public static String getDate(long milliSeconds) {
    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM/yyyy");
    String dateString = formatter.format(new Date(milliSeconds));
    return dateString;
}
Run Code Online (Sandbox Code Playgroud)

java datetime simpledateformat datetime-conversion

0
推荐指数
1
解决办法
3160
查看次数

Java中如何获取两个日期之间的工作日

我的问题是我需要获取用户输入的两个日期之间的工作日。我不知道用什么来比较和获取日期,但有我的代码。

    // Scanner
    Scanner input = new Scanner(System.in);
    System.out.println("First date: ");
    String date1 = input.nextLine();
    System.out.println("Second date: ");
    String date2 = input.nextLine();
    input.close();
    
    // The format for the Date
    DateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
    
    // Conver String to Date
    Date date_1 = formatDate.parse(date1);
    Date date_2 = formatDate.parse(date2);
    
    // Calendar
    Calendar calendar = Calendar.getInstance();
    
    // Date to Calendar
    calendar.setTime(date_1);
    calendar.setTime(date_2);
Run Code Online (Sandbox Code Playgroud)

现在我需要做一些事情来获得这些日期之间的工作日,但我不知道如何

java date

0
推荐指数
1
解决办法
2821
查看次数

如何在 Android 上正确处理日期

我正在使用 Kotlin 开发一个 Android 项目,我们需要对设备和后端的日期进行验证。到目前为止,我们一直在使用 Date 类来表示日期,使用 SimpleDateFormat 类来解析字符串和格式化日期,以及使用 Calendar 类对日期执行操作,例如获取 x 天前的日期等。我发现一些资源指出处理日期的正确方法是使用 java.time 包(LocalDateTime、LocalTime 等类)。我有兴趣确切地知道处理日期的正确方法(从解析到格式化)以及是否需要考虑时区。我见过类似的问题,但没有一个真正概括了我想知道的内容。

java android date kotlin java-time

0
推荐指数
1
解决办法
1177
查看次数

如何以英语和阿拉伯语显示日期格式?

您好,我想实现这样的日期格式“2021 \xd9\x86\xd9\x88\xd9\x81\xd9\x85\xd8\xa8\xd8\xb1”\n有人可以帮助我如何实现这种格式吗?

\n

这是我的示例源代码

\n
 SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMM yyyy", locale);\n            dateString = dateFormatter.format(date);\n
Run Code Online (Sandbox Code Playgroud)\n

datetime android date arabic simpledateformat

0
推荐指数
1
解决办法
3869
查看次数

如何从kotlin中的设备获取当前时间?

如何从设备获取当前时间?我可能需要 kotlin 语言的方法。

android kotlin

0
推荐指数
1
解决办法
5315
查看次数

将yyyy-MM-dd'T'HH:mm:ss.mmm'Z'转换为正常的"HH:mm a"格式

我在应用程序中显示日期时遇到问题.

我得到时间戳为:

2017-08-02T06:05:30.000Z

但就此而言,实际时间是:

2017:08:02 11:35 AM

但在使用我的代码转换后,它将时间显示为:

上午6:00

如何显示当前时间?

我的代码如下:

private static SimpleDateFormat timestampformat = 
                   new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");

private static SimpleDateFormat sdftimeformat = new SimpleDateFormat("HH:mm a");

private static SimpleDateFormat getSdftimeformat() {
    return sdftimeformat;
}
public static String timeStampConvertToTime(String time) {
    Date date1 = null;

    try {
        date1 = timestampformat.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String formattedTime = getSdftimeformat().format(date1);
    return formattedTime;
}
Run Code Online (Sandbox Code Playgroud)

java datetime android timestamp simpledateformat

-2
推荐指数
1
解决办法
1万
查看次数