1

I am trying to convert current UTC time (time from my Linux server) using below code.

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class UtcToIst {

    public static void main(String[] args) {
        List<String> timeZones = new ArrayList<String>();
        String ISTDateString = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String utcTime = sdf.format(new Date());
        System.err.println("utcTime: " + utcTime);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String pattern = "dd-MM-yyyy HH:mm:ss";
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(pattern);
        try {
            String formattedDate = formatter.format(utcTime);
            Date ISTDate = sdf.parse(formattedDate);
            ISTDateString = formatter.format(ISTDate);
            timeZones.add(utcTime+ ","+ ISTDateString);
        } catch(Exception e) {
            e.printStackTrace();
        }

        for(String i: timeZones) {
            System.out.println(i);
        }
    }
}

When I execute the code, I get the below exception:

utcTime: 05-11-2018 12:55:28
java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(DateFormat.java:310)
    at java.text.Format.format(Format.java:157)
    at UtcToIst.main(UtcToIst.java:21)

I see that the UTC time being fetched correctly as: 05-11-2018 12:55:28 But the code is unable to parse the string into IST(Indian Standard Time). I am unable understand how can I fix the problem. Could anyone let me know what is the mistake I am making here and how can I sort it out ?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Metadata
  • 2,127
  • 9
  • 56
  • 127
  • `utcTime` is not a `Date`, it's a `String`, hence the error... – Benoit Nov 05 '18 at 13:09
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 05 '18 at 13:50
  • How come you seem to have missed that this question has been asked and answered a hundred times before? – Ole V.V. Nov 05 '18 at 13:55
  • 1
    Tip: Paste your exception message into your search engine. It very often leads to a good solution. – Ole V.V. Nov 05 '18 at 14:07
  • 2
    You should really follow the **Java Naming Conventions**: variable names always start with lowercase. – MC Emperor Nov 05 '18 at 14:14
  • @OleV.V. Sure. Will do. – Metadata Nov 08 '18 at 09:24

3 Answers3

2

This line is useless and causes the error (utcTime is not a Date, it's a String).

       String formattedDate = formatter.format(utcTime);

Just replace:

        String formattedDate = formatter.format(utcTime);
        Date ISTDate = sdf.parse(formattedDate);

With:

        Date ISTDate = sdf.parse(utcTime);

Whole class:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class UtcToIst {

    public static void main(String[] args) {
        List<String> timeZones = new ArrayList<String>();
        String ISTDateString = "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String utcTime = sdf.format(new Date());
        System.err.println("utcTime: " + utcTime);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String pattern = "dd-MM-yyyy HH:mm:ss";
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(pattern);
        try {
            Date ISTDate = sdf.parse(utcTime);
            ISTDateString = formatter.format(ISTDate);
            timeZones.add(utcTime+ ","+ ISTDateString);
        } catch(Exception e) {
            e.printStackTrace();
        }

        for(String i: timeZones) {
            System.out.println(i);
        }
    }
}
Benoit
  • 5,118
  • 2
  • 24
  • 43
0

use LocalDateTime instead

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));
tornike
  • 76
  • 3
0

Note:the @Benoit's answer produce the incorrect time result if the server timezone is not Indian timezone, Should set timezone to Indian explicitly.

First: Set correct timezone on SimpleDateFormat, Asia/Kolkata for Indian time.

Second: use utc formatter to parse the formatted time string to get the utc time instance.

Last: use Indian to format the utc time instance.

See below code:

SimpleDateFormat utcFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String utcTime = utcFormatter.format(new Date());
System.err.println("utcTime: " + utcTime);

Date utcTimeInstance = utcFormatter.parse(utcTime);
SimpleDateFormat indianFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
indianFormatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String indianTime = indianFormatter.format(utcTimeInstance);
System.err.println("indianTime: " + indianTime);

On my pc prints:

utcTime: 05-11-2018 13:42:31
indianTime: 05-11-2018 19:12:31
Hong Tang
  • 2,334
  • 1
  • 6
  • 7