0

Here is my code.

public String getDateTime()
{


    String dateAndTime = 

    (new SimpleDateFormat("hh:mm aaa")).format(new Date());

    return dateAndTime;


}

public String getDate()
{

        android.text.format.DateFormat df = new android.text.format.DateFormat();
        String Date = df.format("MM-dd-yyyy", new java.util.Date()).toString();

        return  Date;
}

I have searched about this. but, i cant find the perfect answer. Please help me.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Nirmal
  • 2,340
  • 21
  • 43
  • 1
    Danger - time zone abbreviations like `EST` can be ambiguous! `EST` isn't as dangerous as others, like `CST` which might mean "Central Standard Time", or "China Standard Time"! [See this list](http://www.timeanddate.com/library/abbreviations/timezones/) – Matt Johnson-Pint May 01 '13 at 14:52
  • EST can be dangerous enough if your time happens to fall in the time of year when summer time (DST) in effect (which is the greater part of the year). Then you won’t know whether this will be taken into consideration or not, since the correct three letter abbreviation would have been EDT (which in turn *is* ambiguous, it may also refer to Australian Eastern Daylight Time). – Ole V.V. Jun 15 '17 at 10:31

2 Answers2

4

You can use the below function

    private Date shiftTimeZone(Date date, TimeZone sourceTimeZone, TimeZone targetTimeZone) {
    Calendar sourceCalendar = Calendar.getInstance();
    sourceCalendar.setTime(date);
    sourceCalendar.setTimeZone(sourceTimeZone);

    Calendar targetCalendar = Calendar.getInstance();
    for (int field : new int[] {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND}) {
        targetCalendar.set(field, sourceCalendar.get(field));
    }
    targetCalendar.setTimeZone(targetTimeZone);
    System.out.println("........"+targetCalendar.getTimeZone());
    return targetCalendar.getTime();
  }

Usage:

    Date date= new Date();
    SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    sf.format(date);
    TimeZone tz = TimeZone.getTimeZone("GMT") OR TimeZone tz =  sf.getTimeZone();      
    TimeZone tz1 = TimeZone.getTimeZone("EST");     
    Date c= shiftTimeZone( date,tz,tz1);
    System.out.println("Format :   " + sf.format(c));

Output sun.util.calendar.ZoneInfo[id="EST",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

    Format :   01-05-2013 16:23:57
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @Nirmal did u try the above? – Raghunandan May 01 '13 at 07:19
  • s. but i din't get the exact things. I got an output from here dude http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java. thanks for your effort dude. – Nirmal May 01 '13 at 07:22
  • @Nirmal what do you mean by "din't get the exact things".? – Raghunandan May 01 '13 at 07:24
  • it returns exactly. but here http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java it gives the simple code. – Nirmal May 01 '13 at 07:31
  • @Nirmal thanks for accepting. The above code give u time and date in EST format – Raghunandan May 01 '13 at 07:32
  • its ok.. public String getDate() { final SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm aaa"); sdf.setTimeZone(TimeZone.getTimeZone("EST")); final String utcTime = sdf.format(new Date()); return utcTime; } this code is enough to do this process. anyway Thanks For your effort. – Nirmal May 01 '13 at 07:32
1

You can find your answer here :

Date and time conversion to some other Timezone in java

You have to use TimeZone class and Calendar class.

Get current time :

Calendar currentdatetime = Calendar.getInstance();

Just pass your time zone name in TimeZone class like this :

TimeZone.getTimeZone("EST");

Use DateFormater

DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

Then format your time like this :

formatter.setTimeZone(obj);

and get output like this :

System.out.println("EST Time is : "+ formatter.format(currentdatetime .getTime())
Community
  • 1
  • 1
Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76