2

How to convert DateTime to timestamp in android?

String str_offerbegin = start_time;

SimpleDateFormat df_offerbegin = new SimpleDateFormat("MMM dd, yyyy HH:mmaa");

df_offerbegin.setTimeZone(TimeZone.getTimeZone("UTC"));

but not getting exact time and date?

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
Govind
  • 43
  • 1
  • 9

3 Answers3

0

This is literally the first thing that popped up when I googled your question. Try to do some effort before asking a question

DateTime to Timestamp

Maybe these can help you:

convert-date-time-for-given-timezone-java convert-date-to-different-timezone

Example

DateFormat formatter= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss Z");
  formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
  System.out.println(formatter.format(date));

  formatter.setTimeZone(TimeZone.getTimeZone("Europe/Athens"));
  System.out.println(formatter.format(instance2.getTime()))
Community
  • 1
  • 1
Lunchbox
  • 1,538
  • 2
  • 16
  • 42
-1

Try this

Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(str_offerbegin);
Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
  • I tried this one... String str_offerbegin = start_time; SimpleDateFormat df_offerbegin = new SimpleDateFormat( "MMM dd, yyyy HH:mmaa"); df_offerbegin.setTimeZone(TimeZone.getTimeZone("UTC")); Date date_offerbegin = df_offerbegin.parse(str_offerbegin); long epoch_offerbegin = (date_offerbegin.getTime())/1000; – Govind Apr 09 '14 at 06:45
  • Using above code I was unable to get the current local time... Thanks in advance – Govind Apr 09 '14 at 06:48
-1

I suggest you simple set default timezone in UTC by following way,

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));   // for (GMT+5.30)

Above line base on your need any one you add before following code, Now you can access date with default timezone.

String str_offerbegin = start_time;
SimpleDateFormat df_offerbegin = new SimpleDateFormat("MMM dd, yyyy HH:mmaa");
df_offerbegin.parse(str_offerbegin);

Another best helpfull JodaTime Class. Joda Time provides a quality replacement for the Java date and time classes. Using Joda Time,

String str_offerbegin = start_time;
DateTimeZone timeZone = DateTimeZone.forID("Asia/Kolkata");

DateTime datetime = new DateTime(str_offerbegin,timeZone);
String resultdate = datetime.toString();   // Result in String

DateTime dateutc = datetime.withZone(DateTimeZone.UTC);

Hope this help you!

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76