0

I have a code that displays time in GMT and I need to show it in PST. How can I modify the below code to get the time in PST?

Code

public static final String FORMAT = "MM/dd/yyyy";
public static final String OUTPUT_FORMAT_STD_DATE6 = "MM/dd/yy hh:mm a";
public static final String INPUT_FORMAT_STD_TIMESTAMP = "yyyy-MM-dd HH:mm:ss";

public static String formatDate(String strDate, String inputFormat, String outputFormat) {
    Date date = convertStringToDate(strDate,inputFormat);
    String displayDateString = formatDate(date, outputFormat);
    return displayDateString;
}

formatDate is being called here

public void endElement(String uri, String localName, String qName) throws SAXException {
    if( EVENT.equalsIgnoreCase( qName ) ) {
        auditEntries.add(entry);
    } else if( LOG_TIME.equalsIgnoreCase( qName ) ) {
        String time = content.toString();
        entry.setLogTime( DateUtils.formatDate(time, "yyyy-MM-dd'T'HH:mm:ss", DateUtils.OUTPUT_FORMAT_STD_DATE6));
}

Please help, I am new b in writing Java code.

This is how I am doing it.

public static String formatDate(String strDate, String inputFormat, String outputFormat) {
    Date date = convertStringToDate(strDate,inputFormat);

    DateFormat pstFormat = new SimpleDateFormat( outputFormat );
    pstFormat.setTimeZone( TimeZone.getDefault() );
    String displayDateString = formatDate(date, outputFormat);
    return displayDateString;
}

Thanks

Mike
  • 777
  • 3
  • 16
  • 41
  • Is it always GMT to PST? If so the easiest way might be to just modify the date created in the `formatDate()` function directly by subtracting 7 hours. – twain249 Jun 02 '17 at 19:15
  • You mean date - 7? – Mike Jun 02 '17 at 19:18
  • @Mike See [this](https://stackoverflow.com/questions/35212049/how-to-convert-time-from-gmt-to-pst) link, as well as [this](https://stackoverflow.com/questions/13285904/how-to-convert-pst-to-gmt-0800-in-java) link. – ack Jun 02 '17 at 19:21
  • well you can use timezones (which is cleaner) line @AlexQuilliam shows in his link. Or use `setTime()` and `getTime()` to get the time in the seconds since EPOCH and subtract 7*3600 seconds. – twain249 Jun 02 '17 at 19:24
  • 1
    Please provide a [mcve]. Be sure that anyone can copy and paste your code and run it exactly as it is. Also describe how your current output differs from what you want. – Code-Apprentice Jun 02 '17 at 19:26
  • Please provide the output that this code gives you as I can not get this to run on my end. Also, do you want to change the String date or make a date object? – Brent Knox Jun 02 '17 at 19:40
  • 1
    @Brent, this is how I am receiving it after the change 2017-06-02T19:19:17.000 – Mike Jun 02 '17 at 20:02

2 Answers2

0

Once you have a Date object you can convert it to a timezoned string using SimpleDateFormat. Below is a simple example.

Using 3 letter timezone IDs has been deprecated, so you should use GMT+-hour:minute format, or region specific string like America/Los_Angeles as suggested by @VGR

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

Ref:https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html

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

public class DateTest {
    public static final String OUTPUT_FORMAT_STD_DATE6 = "MM/dd/yy hh:mm a";

    public static void main(String[] args) {
        Date date = new Date();

        String gmtDateStr = dateToTimzoneString(date, "GMT", OUTPUT_FORMAT_STD_DATE6);
        System.out.println(gmtDateStr);


        //3 Letter String usage is deprecated
        String pstDateStr = dateToTimzoneString(date, "PST", OUTPUT_FORMAT_STD_DATE6);
        System.out.println(pstDateStr);

        //Correct way
        pstDateStr = dateToTimzoneString(date, "GMT+5", OUTPUT_FORMAT_STD_DATE6);
        System.out.println(pstDateStr);
    }

    public static String dateToTimzoneString(Date date, String timeZoneStr, String outputFormat){
        SimpleDateFormat sd = new SimpleDateFormat(outputFormat);
        sd.setTimeZone(TimeZone.getTimeZone(timeZoneStr));
        return sd.format(date);
    }
}
11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • You misunderstood the TimeZone documentation. The three-letter IDs are indeed deprecated, but that does not mean code should only use the GMT±hh:mm notation. There are plenty of valid [tz database](https://en.wikipedia.org/wiki/Tz_database) IDs, like `"America/Los_Angeles"`, as the documentation points out. – VGR Jun 02 '17 at 20:06
  • Your solution shows me the next day instead of current date. – Mike Jun 02 '17 at 20:28
  • It's possible, suppose you live in a country which is in `GMT-4` and current time is `10 pm` in your country. Then current date in GMT would give you `2 am` next day. – 11thdimension Jun 02 '17 at 21:26
  • But i need to shoe it in PST. I get a time in GMT so I want to convert it to PST from 9 pm to 2 pm. – Mike Jun 02 '17 at 22:12
  • Do you have date as string? – 11thdimension Jun 02 '17 at 22:13
  • If I pass String it throws exception. No I don't. – Mike Jun 02 '17 at 22:26
  • This is how I am doing it now. public static String formatDate(String strDate, String inputFormat, String outputFormat) { Date date = convertStringToDate( strDate,inputFormat ); DateFormat pstFormat = new SimpleDateFormat( outputFormat ); pstFormat.setTimeZone( TimeZone.getTimeZone( "PST" ) ); return pstFormat.format( date ); } – Mike Jun 02 '17 at 22:37
  • You have to check the `convertStringToDate`, if it's taking the `Timezone` into account. – 11thdimension Jun 03 '17 at 03:04
0

This is how I did it.

public static String convertUTC( String strDate, String inputFormat, String outputFormat ) {
    String displayDateString = null;

    try {
        DateFormat inFormat = new SimpleDateFormat( inputFormat );
        inFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
        Date date = inFormat.parse( strDate );

        DateFormat outFormat = new SimpleDateFormat( outputFormat );
        outFormat.setTimeZone( TimeZone.getDefault() );

        displayDateString = formatDate(date, outputFormat);
    } catch ( ParseException pe ) {
        pe.printStackTrace();
    }

    return displayDateString;
}
Mike
  • 777
  • 3
  • 16
  • 41