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.
Related
I want to display the time in the following format for Japanese "01:00 午後"
My current source code is as follows
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
System.out.println("Start Time "+SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,Locale.JAPANESE).format(date));
I am getting the following output
Start Time 7:09:37
P.S: I have also tried with setting the time format to "K:mm a" and "H:mm a" . Both of them didn't work.
You have created object of SimpleDateFormat but not using it. Provide Locale to this class and see the output.
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy a", Locale.JAPANESE);
System.out.println("Start Time "+ sdf.format(date));
Output is : Start Time 10/16/2015 午後
Or use H:mm a DateFormat instead of MM/dd/yyyy a to get the desired out which in case is Start Time 13:01 午後
Refer this document to know more about the Date Formats in Java.
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));
With the following code-
Timestamp ts = (Timestamp)results.get(0);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss Z");
System.out.println(sdf.format(new Date(ts.getTime())));
I get output as: 04/29/2013 15:08:30 +0530
I wanted to create a TimeZone instance from the timestamp, so tried this-
SimpleDateFormat FORMATTER = new SimpleDateFormat("Z");
String tzString = FORMATTER.format(ts);
// the tzString comes out to be +0530 (which is correct)
TimeZone tz = TimeZone.getTimeZone(tzString);
System.out.println(tz);
But the final TimeZone instance is of GMT as its not able to identify +0530.
So, how can I get a correct TimeZone instance here?
You cannot get a TimeZone from a java.sql.Timestamp because it does not contain one. In your case you are simply getting your default TimeZone. It does not make sense. It is the same as TimeZone.getDefault();
Use a lowercase z in your pattern. That should return "GMT+0530", which will work.
Instead of using a SimpleDateFormat, you can simply do this:-
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(ts.getTime()));
TimeZone tz = cal.getTimeZone();
System.out.println(tz);
I tried this code below:
Timestamp ts = new Timestamp(System.currentTimeMillis());
Then to get the TimeZone instance from the timestamp, I did this:
SimpleDateFormat FORMATTER = new SimpleDateFormat("Z");
TimeZone tzone = FORMATTER.getTimeZone();
System.out.println(tzone.getDisplayName());
System.out.println(tzone.getID());
I got:
Central European Time
Europe/Berlin
So I got my timezone which is +0200 instead of GMT.
Hope this is what you want.
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
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("PST"));
cal.setTime(new Date());
System.out.println("cal:"+cal);
System.out.println("cal.getime:"+cal.getTime());
output are:
cal:java.util.GregorianCalendar[time=1325177592164,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="PST",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=PST,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=11,WEEK_OF_YEAR=53,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=363,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=53,SECOND=12,MILLISECOND=164,ZONE_OFFSET=-28800000,DST_OFFSET=0]
cal.getime:Thu Dec 29 22:23:12 IST 2011
Problems facing:
While print 'cal' object getting date and time as per time zone.
cal.getTime() not displaying date and time to as per timezone.
You should represent time in UTC (java.util.Date) and then display the time in the local timezone of the user. Use DateFormat and TimeZone to do that. Read this article for more details
Unfortunately Date object always display time in GMT. You need to use something like SimpleDateFormat to display time in formatted timezone.
assign user given time into string variable then create date formatter. then parse the given string using date formatter.
String dateAndTime = "30-12-2011 12:00:00 GMT+5.30"; dateFormate
formate = new SimpleDateFormate("dd-mm-yyyy hh:mm:ss z"); Date date =
formate.parse(dateAndTime );
Date object is Java doesn't store the TimeZone info.
If you do
new Date()
this is the current local time of the machine/jvm.
To print it with the default/local timezone info:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
System.out.println(sdf.format(new Date()));