📜  如何在Java中将当地时间转换为 GMT?

📅  最后修改于: 2022-05-13 01:55:37.282000             🧑  作者: Mango

如何在Java中将当地时间转换为 GMT?

如果本地人在工作或任何目的方面与海外有联系,则必须将时间从 IST 或任何标准时间转换为 GMT,以便当地人了解并回报他们的国际客户。今天我们将看看一个代码,我们将任何国家的标准时间转换为 GMT。

在这里,我们将使用SimpleDateFormat将本地时间转换为 GMT。如上所述,它在课堂上可用:

java.util.SimpleDateFormat

方法:可以使用不同的方法,例如 SimpleDateFormat 甚至Instance()方法。它们是非常简单和有用的方法。我们也可以使用日历和时间方法来做到这一点。

  1. 使用 SimpleDateFormat 类的format()方法
  2. 使用 SimpleDateFormat 类的instance()方法

方法一:使用 SimpleDateFormat 类的format()方法

Java中DateFormat 类format()方法用于将给定的日期格式化为 日期/时间字符串。基本上该方法用于将此日期和时间转换为特定格式,即 mm/dd/yyyy。

句法:

public final String format(Date date)

参数:该方法采用Date对象类型的一个参数date,指的是要产生字符串输出的日期。

返回值:该方法以mm/dd/yyyy 的字符串格式返回日期或时间。

程序:

  • 在这里,我们将简单地首先打印我们的本地时间
  • 然后使用 SimpleDateFormat 将其转换为 GMT
  • 然后打印两个时区。

例子:

Java
// Java Program to convert local time to GMT
 
// Importing libraries
// 1. input output libraries
import java.io.*;
// 3. Text class
import java.text.DateFormat;
import java.text.SimpleDateFormat;
// 2. Utility libraries for
// Date and TimeZone class
import java.util.Date;
import java.util.TimeZone;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a Date class object
        // to take local time from the user
        Date localTime = new Date();
 
        // Creating a DateFormat class object to
        // convert the localtime to GMT
        DateFormat s = new SimpleDateFormat("dd/MM/yyyy"
                                            + " "
                                            + " HH:mm:ss");
 
        //  function will helps to get the GMT Timezone
        // using the getTimeZOne() method
        s.setTimeZone(TimeZone.getTimeZone("GMT"));
 
        // One can get any other time zone also
        // by passing some other argument to it
 
        // Printing the local time
        System.out.println("local Time:" + localTime);
 
        // Printing the GMT time to
        // illustrate changes in GMT time
        System.out.println("Time IN Gmt : "
                           + s.format(localTime));
    }
}


Java
// Java Program to convert Local time to
// GMT time
 
// Importing all input output classes
import java.io.*;
// Importing all time classes
import java.time.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Instant operator helps to note
        // the time and the location of it
 
        // Creating an object of Instant type
        // using the now() method
        Instant now = Instant.now();
 
        // Now with the help of Instant operator
        // zoned operator is called
 
        // Creating an object of ZonedDateTime
        ZonedDateTime zdt = ZonedDateTime.ofInstant(
            now, ZoneId.systemDefault());
 
        // Printing the local time
        System.out.println(" Local : " + zdt);
 
        // Creating an object of  Instant type
        // taking any other instant method
        Instant instant = Instant.now();
 
        // Printing the GMT/UTC time by parsing with string
        // using the toString() method
        System.out.println(" GMT : "+instant.toString());
    }
}



输出
local Time:Thu Feb 04 11:34:15 UTC 2021
Time IN Gmt : 04/02/2021  11:34:15

方法二:使用 SimpleDateFormat 类的instance()方法

正如在上面的方法中我们使用了 SimpleDateFormat,我们现在将使用即时方法来获取时间。 SimpleDateFormat 可以以不同的方式使用,现在可以使用即时方法来获取 UTC 或 GMT。

程序:

  • 我们将使用Instant方法来获取正确的时间
  • 它可以导入完整的时间类
java.time.* ;

例子:

Java

// Java Program to convert Local time to
// GMT time
 
// Importing all input output classes
import java.io.*;
// Importing all time classes
import java.time.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Instant operator helps to note
        // the time and the location of it
 
        // Creating an object of Instant type
        // using the now() method
        Instant now = Instant.now();
 
        // Now with the help of Instant operator
        // zoned operator is called
 
        // Creating an object of ZonedDateTime
        ZonedDateTime zdt = ZonedDateTime.ofInstant(
            now, ZoneId.systemDefault());
 
        // Printing the local time
        System.out.println(" Local : " + zdt);
 
        // Creating an object of  Instant type
        // taking any other instant method
        Instant instant = Instant.now();
 
        // Printing the GMT/UTC time by parsing with string
        // using the toString() method
        System.out.println(" GMT : "+instant.toString());
    }
}


输出
Local : 2021-02-04T10:40:34.436700Z[Etc/UTC]
 GMT : 2021-02-04T10:40:34.547680Z