Rinkesh
Rinkesh

Reputation: 3316

Time convert to timestamp

I have a log in timeformat

31/Mar/2013:17:03:30 -0700

I want to convert it into a timestamp here -70 in timezone. How can I do that?

try {
    String time = myMap.get("timestamp");
    String splitTime[] = time.split("-");//input Timestamp 31/Mar/2013:17:03:30 -0700
    Date date = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss").parse(splitTime[0]);
    myMap.put("timestamp", String.valueOf(new Long(date.getTime() / 1000)));
} catch (ParseException e) {
     e.printStackTrace();
}

How do I use this timezone?

Upvotes: 0

Views: 108

Answers (2)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136112

try this

Date date = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z").parse(s);

note that it will only parse /Mar/ if your default language is English, otherwise use

Date date = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US).parse(s);

Upvotes: 5

Abubakkar
Abubakkar

Reputation: 15664

Well you can use that time zone information to subtract/add that much time to your date:

String time = myMap.get("timestamp");
String splitTime[] = time.split("-");//input Timestamp 31/Mar/2013:17:03:30 -0700
Date date = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss").parse(splitTime[0]);

long timestamp = date.getTime() / 1000;  ///datetime in seconds

long timezonehour = Long.parseLong(splitTime[1].substring(0,1));   // 07
long timezoneminutes = Long.parseLong(splitTime[1].substring(2,3));  // 00

timezonehour +=  timezoneminutes/60;

long timezone_seconds = (timezonehour/60)/60;   // in seconds

timestamp += timezone_seconds // final computed value

I know this looks ugly but can't help further.

Upvotes: 1

Related Questions