当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Local Time转GMT用法及代码示例

如果当地人因工作或任何目的而与海外联系,则需要从 IST 或任何标准时间转换为 GMT 时间,以便当地人理解并返回他们的国际客户。今天我们将看一下将任何国家/地区的标准时间转换为 GMT 的代码。

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

java.util.SimpleDateFormat

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

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

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

SimpleDateFormat format()的方法DateFormat 类在Java中用于将给定的日期格式化为 日期/时间字符串。本质上,该方法用于将此日期和时间转换为特定格式,即 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));
    }
}


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

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

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

程序:

  • 我们将使用立即的获取正确时间的方法
  • 可以导入完整的时间类
 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


相关用法


注:本文由纯净天空筛选整理自saransh9342大神的英文原创作品 How to Convert Local Time to GMT in Java?。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。