Calender issue with time zone - java

I'm having a weird situation with Java Calendar. I'm using dozer mapper to map the objects.
I want to write a method that will convert this object to the following format. yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
say element 2010-11-11T09:30:47.000Z
public Calender getValue(Date source,Calender c) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTime(source);
return calendar;
}
When I run the program, it is printing
2010-11-11T04:00:47.000Z - Because we are setting the Timezone to be GMT, (9.30 - 5.30 = 4.00)
I want my object to have same format and value.if I don't set TimeZone to GMT, it will show as 2008-11-21T09:30:47.000+05:30.
I want it as 2010-11-11T09:30:47.000Z.
I tried added 5.30 to calender.
calendar.add(Calendar.HOUR, 5);
calendar.add(Calendar.MINUTE, 30)
then it works.But if this is ran from any other place, difference won't be 5.30.So I cannot add 5.30 to calenderget
Is there any way to get rid of this problem? I want to return Calender object.
Any suggestions or help would be much appreciated

Use a pattern. F.E:
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
Also,
SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);

you can use SimpleDateFormat like this.
SimpleDateFormat formatter, FORMATTER;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String oldDate = "2011-03-10T11:54:30.207Z";
Date date = formatter.parse(oldDate.substring(0, 24));
FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
System.out.println("OldDate-->"+oldDate);
System.out.println("NewDate-->"+FORMATTER.format(date));
Output OldDate-->2011-03-10T11:54:30.207Z NewDate-->10-Mar-2011 11:54:30.207

Related

java date daylight saving

I just printed new Date() in my box, and it always returns the older time which corresponds to EST, however the date command in the box returns the exact time after moving to EDT, IS there anything that need to be done for the new Date() to return the exact date ? I do not want any alternate java commands, but want the Date to work as it is expected, Am I missing something ?
Thank you
try this:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone.
Hi the below code would be useful for your case.
SimpleDateFormat df = new SimpleDateFormat("dd MMM, yyyy HH:mm:ss z"); //Date format
df.setTimeZone(TimeZone.getTimeZone("EDT")); //set Timezone
df.format(new Date());
Thanks.

convert a date object to Oracle timestamp type string

I want to convert a date object, ex: new Date(), to a string which has a format like Oracle's time stamp type, ex: 21-OCT-13 11.08.13.858000000 AM. I know I could just get each piece of information in the date object like day, month, year, hour, minute, ... to form the Oracle format string but I really want to know is there a utility to do that instead?
Using SimpleDateFormat#format() you would print a Date as
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
System.out.println(sdf.format(new Date()).toUpperCase());
Output :
21-OCT-13 10.01.38.000000614 AM
See JavaDocs for Date and Time patterns.
Try taking a look at SimpleDateFormats - That would be your best bet and easiest way of doing it.
Eg:
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); //Hours:Minutes:Seconds
String strDate = dateFormat.format(date);
Use SimpleDateFormat.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("your_format_here"); // dd/MM/yy h:mm:ss a
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

trouble with local time

i use these codes to get my local time:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar currenDdate = Calendar.getInstance(TimeZone.getDefault());
System.out.print("Last update: "+dateFormat.format(currenDdate.getTime()));
or:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar currenDdate = Calendar.getInstance(getTimeZone("GMT+3.5"));
System.out.print("Last update: "+dateFormat.format(currenDdate.getTime()));
but non of them give the local time, they are both giving me GMT
what is the problem?
NOTE: I am using eclipse and android programming, the codes above correctly work in java but in android it is not!
instead of system.out.print() i use a textview to show the time
TextView date = (TextView) findViewById(R.id.gold_textView1);
date.setText("Last update: "+dateFormat.format(currenDdate.getTime()));
please help me find the problem
Try Date currentDate = new Date() instead of the Calendar object. Your System.out.print() call won't change. Also, double check your system to make sure its default timezone is set correctly.
Have you tried like this:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getDefault());
Calendar currenDdate = Calendar.getInstance(TimeZone.getDefault());
System.out
.print("Last update: " + dateFormat.format(currenDdate.getTime()));
Instead of Calendar you need to set your TimeZone on SimpleDateFormat:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0330"));
// prints: Last update: 2013/07/31 18:46:24 +0330
System.out.println("Last update: " + dateFormat2.format(currenDdate.getTime()));
This is required because SimpleDateFormat uses a Calendar instance of its own internally.
See SimpleDateFormat#setTimeZone()
public void setTimeZone(TimeZone zone)
{
calendar.setTimeZone(zone);
}
Notice, the date format pattern also includes the time zone letter Z to include it in the display.
Try this, use JodaTime http://www.joda.org/joda-time/
DateTimeZone dateTimeZoneUTC = DateTimeZone.UTC;
DateTime dateTimeUTC = new DateTime().withZone(dateTimeZoneUTC);
DateTimeZone dateTimeZoneLocal = DateTimeZone.getDefault();
DateTime dateTimeLocal = dateTimeUTC.withZone(dateTimeZoneLocal);
DateTimeZone dateTimeZoneOffset = DateTimeZone.forID("-03:00");
DateTime dateTimeOffset = dateTimeLocal.withZone(dateTimeZoneOffset);
// Airport Current Time hh/mm
DateTimeFormatter timeFormat = DateTimeFormat.forPattern("hh/mm").withZone(dateTimeZoneOffset);
currentTime.setText(timeFormat.print(dateTimeOffset));

Calculating Date in Java

Ok so I am trying to create a date in this format:
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
I am having trouble calculating that date so that it gives me 1/1/13.
Date newdate = new Date (136199001);
String date = dateformat.format(newdate);
However I can't work out how to do it to get to my desired date. I know I am suppose to work it out from 01/01/70 but I am having trouble. The question : what is the formula to work the date out?
I would say that what you are looking for is this:
new SimpleDateFormat("dd/MM/yy").parse("1/1/13");
You can use calendar object for a specific date. It is much easier.
Calendar cal = Calendar.getInstance();
cal.set(2013, 0, 1); //1st january 2013
Date date = cal.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
String dateStr = dateformat.format(date);

converting a calendar date to a string?

I am trying to get the program to call up the current date, add 30 days to it, and then out put that date as a string.
// Set calendar for due date on invoice gui
Calendar cal = Calendar.getInstance();
// Add 30 days to the calendar for the due date
cal.add(Calendar.DATE, 30);
Date dueDate = cal.getTime();
dueDatestr = Calendar.toString(dueDate);
And the question is?
If you want to format your date, I suggest looking at java.text.SimpleDateFormat instead of using toString(). You can do something like:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dueDateStr = dateFormat.format(dueDate); // renders as 11/29/2009
You almost have it:
Date dueDate = cal.getTime();
String dueDateAsString = dueDate.toString();
or
String dueDateAsFormattedString = DateFormat.format(dueDate);
You might want to consider using FastDateFormat from Apache commons, instead of SimpleDateFormat, because SimpleDateFormat is not thread safe.
FastDateFormat dateFormat = FastDateFormat.getInstance("MM/dd/yyyy");
dueDateStr = dateFormat.format(dueDate);
This is especially true if you wanted to use a static instance of the date formatter, which is a common temptation.
You can do it easily with a class of mine:
https://github.com/knyttl/Maite/wiki/Maite-Date-and-Time
new Time()
.plus(1, Time.DAY)
.format("yyyy-MM-dd");

Categories

Resources