简体   繁体   中英

How to convert DateTime to timestamp in android?

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?

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()))

尝试这个

Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(str_offerbegin);

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM