Sponsored Content
Top Forums Programming Normalizing date value to a single timezone Post 302854759 by matrixmadhan on Wednesday 18th of September 2013 10:22:32 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
CAL(1)							    BSD General Commands Manual 						    CAL(1)

NAME
cal, ncal -- displays a calendar and the date of easter SYNOPSIS
cal [-jy] [[month] year] cal [-j] -m month [year] ncal [-jJpwy] [-s country_code] [[month] year] ncal [-Jeo] [year] DESCRIPTION
The cal utility displays a simple calendar in traditional format and ncal offers an alternative layout, more options and the date of easter. The new format is a little cramped but it makes a year fit on a 25x80 terminal. If arguments are not specified, the current month is dis- played. The options are as follows: -J Display Julian Calendar, if combined with the -e option, display date of easter according to the Julian Calendar. -e Display date of easter (for western churches). -j Display Julian days (days one-based, numbered from January 1). -m month Display the specified month. -o Display date of orthodox easter (Greek and Russian Orthodox Churches). -p Print the country codes and switching days from Julian to Gregorian Calendar as they are assumed by ncal. The country code as deter- mined from the local environment is marked with an asterisk. -s country_code Assume the switch from Julian to Gregorian Calendar at the date associated with the country_code. If not specified, ncal tries to guess the switch date from the local environment or falls back to September 2, 1752. This was when Great Britain and her colonies switched to the Gregorian Calendar. -w Print the number of the week below each week column. -y Display a calendar for the specified year. A single parameter specifies the year (1 - 9999) to be displayed; note the year must be fully specified: ``cal 89'' will not display a calen- dar for 1989. Two parameters denote the month and year; the month is either a number between 1 and 12, or a full or abbreviated name as specified by the current locale. Month and year default to those of the current system clock and time zone (so ``cal -m 8'' will display a calendar for the month of August in the current year). A year starts on Jan 1. SEE ALSO
calendar(3), strftime(3) HISTORY
A cal command appeared in Version 5 AT&T UNIX. The ncal command appeared in FreeBSD 2.2.6. AUTHORS
The ncal command and manual were written by Wolfgang Helbig <helbig@FreeBSD.org>. BUGS
The assignment of Julian--Gregorian switching dates to country codes is historically naive for many countries. BSD
November 23, 2004 BSD
All times are GMT -4. The time now is 09:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy