0

is this possible to get the output in Date type in the below format

> 2014-11-12 09:23:47 GMT+05:30

not to be like

> Wed Nov 12 06:53:47 IST 2014

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @Basil, I don't think OP wants to actually convert the time to a different timezone (as per your suggested dupe). They just want it displayed with `GST+05:30` rather than `IST` (which are the _same_ time zones). And the mods to the date/time format itself of course. – paxdiablo Nov 14 '14 at 07:45
  • 1
    @paxdiablo Both the question and the answers I linked show how to use similar formats for getting output from a Date as requested in this Question. There are *many* more duplicates as well: [this](http://stackoverflow.com/a/18493592/642706) and [this](http://stackoverflow.com/q/4772425/642706) and [this](http://stackoverflow.com/q/5683728/642706) and [this](http://stackoverflow.com/q/10614771/642706) and more. – Basil Bourque Nov 14 '14 at 08:02
  • @BasilBourque thank you for marking as dupe. now it is possible? – Vignesh Babu Nov 14 '14 at 10:23

1 Answers1

1

That can be done using SimpleDateFormat with the format string:

yyyy-MM-dd HH:mm:ss 'GMT'XXX

as per the following program:

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

public class Test {
    public static void main(String[] args) {
        Date dt1 = new Date();
        DateFormat df = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss 'GMT'XXX");
        String line = df.format(dt1);
        System.out.println(line);
    }
}

On my system, that gives me:

2014-11-14 15:36:16 GMT+08:00
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • @Vignesh, not sure I understand the comment. If you want your date output in that specific way, `SimpleDateFormat` is the answer. You don't _have_ to put it in a string, you can just output or use `df.format(dt1)` directly. I'd suggest having a play with the code I provided and experimenting. – paxdiablo Nov 14 '14 at 08:14
  • thank you for ur ans @paxdiablo but i need the output in specific way and in Date type, but df.format(dt1) is string format. – Vignesh Babu Nov 14 '14 at 08:18
  • @Vignesh, a date _has_ no format, it's an abstract data type. You can either use the default provided by its `toString` method or use a date formatter as I've shown. In _both_ cases, the result is a string. Don't confuse the data with the presentation, they're two separate things. – paxdiablo Nov 14 '14 at 08:42