微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用偏移量将字符串日期转换为DateTimePSTString和DateTimeLocalString?

如何解决使用偏移量将字符串日期转换为DateTimePSTString和DateTimeLocalString?

我有三个参数,UTCTime和两个偏移量,所有三个都作为字符串,我需要按如下所示进行转换

gcloud composer environments run `$COMPOSER_ENV` --location <location> connections -- --list

我试图做这样的事情

╒════════════════════════════════╤═════════════════════════════╤════════════════════════════════╤════════╤════════════════╤══════════════════════╤════════════════════════════════╕
│ Conn Id                        │ Conn Type                   │ Host                           │ Port   │ Is Encrypted   │ Is Extra Encrypted   │ Extra                          │
╞════════════════════════════════╪═════════════════════════════╪════════════════════════════════╪════════╪════════════════╪══════════════════════╪════════════════════════════════╡
│ 'airflow_db'                   │ 'MysqL'                     │ 'airflow-sqlp...rvice.default' │ None   │ True           │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'beeline_default'              │ 'beeline'                   │ 'localhost'                    │ 10000  │ False          │ True                 │ 'gAAAAABfdZs0...yjt7nj1C2Dzgm' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'bigquery_default'             │ 'google_cloud_platform'     │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs2...AOdwY-EnZLg==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'local_MysqL'                  │ 'MysqL'                     │ 'localhost'                    │ None   │ True           │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'presto_default'               │ 'presto'                    │ 'localhost'                    │ 3400   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'google_cloud_default'         │ 'google_cloud_platform'     │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs2...oMm2saUwAxQ==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'hive_cli_default'             │ 'hive_cli'                  │ None                           │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'pig_cli_default'              │ 'pig_cli'                   │ None                           │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'hiveserver2_default'          │ 'hiveserver2'               │ 'localhost'                    │ 10000  │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'metastore_default'            │ 'hive_metastore'            │ 'localhost'                    │ 9083   │ False          │ True                 │ 'gAAAAABfdZs0...vNSgFh1mE1HY=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'mongo_default'                │ 'mongo'                     │ 'mongo'                        │ 27017  │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'MysqL_default'                │ 'MysqL'                     │ 'MysqL'                        │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'postgres_default'             │ 'postgres'                  │ 'postgres'                     │ None   │ True           │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'sqlite_default'               │ 'sqlite'                    │ '/tmp/sqlite_default.db'       │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'http_default'                 │ 'http'                      │ 'https://www.httpbin.org/'     │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'mssql_default'                │ 'mssql'                     │ 'localhost'                    │ 1433   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'vertica_default'              │ 'vertica'                   │ 'localhost'                    │ 5433   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'wasb_default'                 │ 'wasb'                      │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...ST7E2347-uG4=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'webhdfs_default'              │ 'hdfs'                      │ 'localhost'                    │ 50070  │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'ssh_default'                  │ 'ssh'                       │ 'localhost'                    │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'sftp_default'                 │ 'sftp'                      │ 'localhost'                    │ 22     │ False          │ True                 │ 'gAAAAABfdZs0...guLrr1ky5XpN2' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'fs_default'                   │ 'fs'                        │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...WqhP9ZLa8gQ==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'aws_default'                  │ 'aws'                       │ None                           │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'spark_default'                │ 'spark'                     │ 'yarn'                         │ None   │ False          │ True                 │ 'gAAAAABfdZs0...18ws2BelkcL8=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'druid_broker_default'         │ 'druid'                     │ 'druid-broker'                 │ 8082   │ False          │ True                 │ 'gAAAAABfdZs0...sC6Kcd9mOKhE=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'druid_ingest_default'         │ 'druid'                     │ 'druid-overlord'               │ 8081   │ False          │ True                 │ 'gAAAAABfdZs0...CpBdCkHuk5lqw' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'redis_default'                │ 'redis'                     │ 'redis'                        │ 6379   │ False          │ True                 │ 'gAAAAABfdZs0...E1qdjhMngIg==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'sqoop_default'                │ 'sqoop'                     │ 'rmdbs'                        │ None   │ False          │ False                │ ''                             │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'emr_default'                  │ 'emr'                       │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...GsJIS8IjaBuM=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'databricks_default'           │ 'databricks'                │ 'localhost'                    │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'qubole_default'               │ 'qubole'                    │ 'localhost'                    │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'segment_default'              │ 'segment'                   │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...oawClUj4Qzj8i' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'azure_data_lake_default'      │ 'azure_data_lake'           │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...DMIAMmOeZNg==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'azure_cosmos_default'         │ 'azure_cosmos'              │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...tusOfGrWviAk=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'azure_contai...ances_default' │ 'azure_container_instances' │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs0...q460BKvTu4Lk=' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'cassandra_default'            │ 'cassandra'                 │ 'cassandra'                    │ 9042   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'dingding_default'             │ 'http'                      │ ''                             │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'opsgenie_default'             │ 'http'                      │ ''                             │ None   │ False          │ False                │ None                           │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'google_cloud...store_default' │ 'google_cloud_platform'     │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs2...ltsxQHWUgxA==' │
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┼────────┼────────────────┼──────────────────────┼────────────────────────────────┤
│ 'google_cloud_storage_default' │ 'google_cloud_platform'     │ None                           │ None   │ False          │ True                 │ 'gAAAAABfdZs2...RNLazPEE7gQ==' │
╘════════════════════════════════╧═════════════════════════════╧════════════════════════════════╧════════╧════════════════╧══════════════════════╧════════════════════════════════╛

我错过了几秒钟,这似乎是一种低效的方式,因为我不需要在输出中使用'T'或偏移量,并且可以摆脱它们。我不确定是否已经问过这个问题。任何帮助表示赞赏。

解决方法

由于给定的日期时间字符串2020-09-29 16:06:00没有区域偏移信息,因此需要先将其解析为LocalDateTime,然后才能将LocalDateTime转换为{{ 1}}。

执行以下操作:

OffsetDateTime

输出:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Define the formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");

        // Given date-time
        String localTimeUTC = "2020-09-29 16:06:00";

        // Get OffsetDateTime from the given date-time string
        OffsetDateTime odtUTC = LocalDateTime.parse(localTimeUTC,formatter).atOffset(ZoneOffset.UTC);

        // OffsetDateTime with an Zone-Offset of -7 hours
        OffsetDateTime odtAtOffsetMinus7Hours = odtUTC.withOffsetSameInstant(ZoneOffset.ofHours(-7));
        System.out.println(odtAtOffsetMinus7Hours.format(formatter));

        // OffsetDateTime with an Zone-Offset of -4 hours
        OffsetDateTime odtAtOffsetMinus4Hours = odtUTC.withOffsetSameInstant(ZoneOffset.ofHours(-4));
        System.out.println(odtAtOffsetMinus4Hours.format(formatter));
    }
}

注意:除了使用2020-09-29 09:06:00 2020-09-29 12:06:00 之外,您还可以根据需要使用的{数字/字符串)使用ZoneOffset.ofHours(-7)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。