Normalizing date value to a single timezone


 
Thread Tools Search this Thread
Top Forums Programming Normalizing date value to a single timezone
# 1  
Old 09-18-2013
Normalizing date value to a single timezone

Hi,

Am trying to get a normalized date value irrespective of the time zone of the machine in which following code is run.

When the following code is run in 2 different machines with TZ=UTC and TZ=PDT, I get 2 different values.

I simply want to normalize the output that is specific to a single timezone, therefore UTC. Be it any timezone, I want the output to be always in UTC. Is there a way to achieve that?

I was hoping,
Code:
output.setTimeZone(TimeZone.getTimeZone("UTC"));

but it didn't.

Code:
        String dateVal = "2007-02-23T20:14:33.123";
        String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
        int daysToAdd = 2;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        simpleDateFormat.setLenient(false);
        Date date = simpleDateFormat.parse(dateVal);
        System.out.println("dateval:" + date.toString());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println("calendar:" + calendar.toString());
        calendar.add(Calendar.DATE, daysToAdd);

        SimpleDateFormat output = new SimpleDateFormat(dateFormat);
        output.setTimeZone(TimeZone.getTimeZone("UTC"));

        System.out.println("final-val:" + output.format(calendar.getTime()));

Any pointers?

Thanks
# 2  
Old 09-18-2013
# 3  
Old 09-19-2013
I did read that and that is the reason I have used that to normalize to a single time zone value whatever be the time zone value in the host from which its being run.

Still couldn't figure out a strategy where timezone is normalized to a single value.

Are you saying Calendar.setTimezone() should not be used?

Thanks for the reply
# 4  
Old 09-19-2013
TimeZone (Java Platform SE 7 )) Can set the default time zone.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (1 Reply)
Discussion started by: b.saipriyanka
1 Replies

2. UNIX for Beginners Questions & Answers

How can we Zip multiple files created on the same date into one single zip file.?

Hi all i am very new to shell scripting and need some help from you to learn 1)i have some log files that gets generated on daily basis example: i have abc_2017_01_30_1.log ,2017_01_30_2.log like wise so i want to zip this 4 logs which are created on same date into one zip folder. 2)Post zipping... (2 Replies)
Discussion started by: b.saipriyanka
2 Replies

3. Shell Programming and Scripting

History of all the users in single file with command , date . time , ip and user

HTML Code: archive_history() { HISTORYOLD=${HISTFILE}.archive CURTIME=`date` CURTTY=`tty` IP=$(echo $SSH_CLIENT | awk '{print $1}') if ; then echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ${USER} ${IP}----" >> $HISTORYOLD history... (0 Replies)
Discussion started by: rehantayyab82
0 Replies

4. Shell Programming and Scripting

History of all the users in single file with command , date . time , ip and user

HTML Code archive_history() { HISTORYOLD=${HISTFILE}.archive CURTIME=`date` CURTTY=`tty` IP=$(echo $SSH_CLIENT | awk '{print $1}') if ; then echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ${USER} ${IP}----" >> $HISTORYOLD history... (2 Replies)
Discussion started by: rehantayyab82
2 Replies

5. Shell Programming and Scripting

Timezone and date +%s command

DST started on Oct 20th in Brazil. If I run the below command in that time zone, date +%s -d "10/20/2013" it throws an error message "Invalid date" . But it works fine for other dates including 19th and 21st Oct. Any idea ? Is there any unix patch ? (2 Replies)
Discussion started by: Lakshmikumari
2 Replies

6. UNIX Desktop Questions & Answers

Normalizing files for sentence count

I have files with many different formats and breaks in odd places. now I want to normalize them to be able to count the sentence in each file 1: I want to count the sentences is they finish with ! . ? 2: but I don't want it to count if there is no space after the Full stop. e.g. S.O.L I have... (6 Replies)
Discussion started by: A-V
6 Replies

7. UNIX for Dummies Questions & Answers

How to set correct timezone and date?

Hi all. I changed the date to go ahead one hour for DST this past weekend and date command shows: /home/oracle>date Wed Mar 17 12:40:12 EST 2010 /home/oracle>echo $TZ Canada/Eastern I think it should be reading EDT and not EST as My Dataprotector is reporting backups at... (5 Replies)
Discussion started by: jamie_collins
5 Replies

8. Shell Programming and Scripting

Single digit date to double digit date.

I have a var storing date var=`date` Now the date is returned as Mon Feb 2 00:25:48 PST 2009 Is there any way to check the date field alone ("2" in above case) and if its a single digit then add a prefix 0 to it and store the result in same variable "var" My intention in above case is... (3 Replies)
Discussion started by: villain41
3 Replies

9. Shell Programming and Scripting

single digit for day and month on date

hi all, how do i format the date command so it displays day and month in single digits i.e 8 instead of 08 ?? am using the command (in a ksh) : date +%D output i get is 10/08/08 thanks in advance. (5 Replies)
Discussion started by: cesarNZ
5 Replies

10. UNIX for Advanced & Expert Users

Timezone

HI ; I got Solaris8 installed oon Sunfire V440 .I have TZ=GMT in /etc/TIMEZONE , but when i log in as root i see the date in GMT+1. So i have every time to "set TZ=GMT ; export $TZ" . How can i set the TZ=GMT at the startup defenitely. Thanks :) (7 Replies)
Discussion started by: oss
7 Replies
Login or Register to Ask a Question