问题描述
我有三个参数,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)
。