I was trying to converted user inputed date in GMT time, user inputed date in 24hour format, when i convert in GMT time it show 12 hour problem, How can i get 24 hour formated GMT time
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String s = starttDatePicker.getYear() + "-"
+ (starttDatePicker.getMonth() + 1) + "-"
+ starttDatePicker.getDayOfMonth() + " "
+ starttimepicer.getCurrentHour() + ":"
+ starttimepicer.getCurrentMinute() + ":" + "00";
Date parsed = null;
try {
parsed = format1.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TimeZone tz = TimeZone.getTimeZone("GMT");
format1.setTimeZone(tz);
posttime = format1.format(parsed);
Change "yyyy-MM-dd hh:mm:ss" to "yyyy-MM-dd HH:mm:ss"
Just to clarify the documentation for formatting date/times mentions that hh is 12 hour time while HH is 24 hour time.
hh is for 12 hrs format and
HH is for 24 hrs format
Related
I want to convert calendar object to date as follow.
int year,month,day;
mCalendarEnd = Calendar.getInstance();
year = mCalendarEnd.get(Calendar.YEAR);
month = mCalendarEnd.get(Calendar.MONTH)+1;
day = mCalendarEnd.get(Calendar.DAY_OF_MONTH);
Now convert it to date object
Date d1 = new Date(day,month,year);
when I print date object:
System.out.println("Date : "+d1.getDay()+"/"+d1.getMonth()+"/"+d1.getYear());
it should print current date but in above code it prints the wrong date. Any idea how can I solve this problem? your all suggestion are appreciable.
You need to do it like this
//Set calendar
Calendar calendar = Calendar.getInstance();
Date date1 = calendar.getTime(); // gives a date object
//To get day difference, Just an example
calendar.add(Calendar.DAY_OF_MONTH, -7);
Date date2 = calendar.getTime(); // gives a date object
long differenceInMillis = Date1.getTime() - Date2.getTime();
long differenceInDays = TimeUnit.DAYS.convert(differenceInMillis, TimeUnit.MILLISECONDS);
Or No need of date objects
Calendar calendar = Calendar.getInstance();
long date1InMillis = calendar.getTimeInMillis();
calendar.add(Calendar.DAY_OF_MONTH, -7);
long date2InMillis = calendar.getTimeInMillis();
long differenceInMillis = date1InMillis - date2InMillis;
long differenceInDays = TimeUnit.DAYS.convert(differenceInMillis, TimeUnit.MILLISECONDS);
calendar.getTimeInMillis()
calendar.getTime() returns Date object.
but if you just need today date new Date() returns today date as Date object, too.
Calendar example:
Calendar calendar = Calendar.getInstance()
Log.i("My Tag", "calendar getTime -----> " + calendar.getTime());
Output:
My Tag: calendar getTime -----> Wed Dec 05 13:03:43 GMT+03:30 2018
Date Example:
Log.i("My Tag", "new Date -----> " + new Date());
Output:
My Tag: new Date -----> Wed Dec 05 13:05:38 GMT+03:30 2018
as you see both of them have the same output.
Why you don't use just the calendar?
System.out.println("Date : " + day + "/" + month + "/" + year);
result 1/1/1900
or you want other format? but dont increment the month with 1
System.out.println("Date : " + day + "/" + new DateFormatSymbols().getMonths()[month] + "/" + year);
result 1/January/1900
Put this in your code:
Date d1 = new Date(year, month, day);
System.out.println("Date : " + d1.getDate() + "/" +d1.getMonth() + "/" + d1.getYear());
you will get the correct date.
d1=new Date(year, month, day);
System.out.println("Dt:"+d1.getDate()+"/"+d1.getMonth()+"/"+d1.getYear());
Android Local time to EST time Conversion
Code:
SimpleDateFormat serverDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
serverDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
Calendar calender= Calendar.getInstance(Locale.getDefault());
String time=serverDateFormat.format(calender.getTime());
but i getting wrong time.
one hour difference from right time.
for eg :
local time : Tue Jul 07 17:30:00 GMT+05:30 2015
formated time : 2015/07/07 07:00:00
right time : 2015/07/07 08:00:00
Your problem is using the identifier "EST" which stands for "Eastern Standard Time". As the name suggests it does not use daylight saving rules. Proof:
TimeZone tz = TimeZone.getTimeZone("EST");
long offset = tz.getOffset(System.currentTimeMillis()) / (1000 * 3600);
System.out.println(tz.getID() + ":" + tz.useDaylightTime() + "/" + offset);
// output: EST:false/-5
Use the timezone id "America/New_York" instead:
tz = TimeZone.getTimeZone("America/New_York");
offset = tz.getOffset(System.currentTimeMillis()) / (1000 * 3600);
System.out.println(tz.getID() + ":" + tz.useDaylightTime() + "/" + offset);
// output: America/New_York:true/-4
Then you will observe daylight saving time in July making an offset difference of (+05:30) - (-04:00) = +09:30 resulting in the expected local time 8 AM.
hey please try this function for time conversion -
public static String getTime(String time, SimpleDateFormat sdf) {
String convertedTime = "";
try {
TimeZone timeZone = TimeZone.getDefault();
Date postdate = sdf.parse(time);
long postTimeStamp = postdate.getTime() + timeZone.getRawOffset();
String dateString = sdf.format(new Date(postTimeStamp));
convertedTime = dateString;
// convertedTime = getLastTime(context, time);
} catch (Exception e) {
e.printStackTrace();
}
return convertedTime;
}
I'm trying to convert the calendar which i set the time milliseconds from 1 1 1970 as below:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(1417780800);
Although when I'm trying to convert this calendar to a string as in this format "yyyy-MM-dd HH:mm:ss" it always set the date as 1970-1-17 9:49:40 when it should be 2014-12-1 12:00:00
I used this way to convert the date:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(c.getTime());
Also, using this way is getting wrong:
String date = "" + c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH) + " " + c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
Any idea why?
Thanks in advance.
You can try something like...
DateFormat.getDateInstance().format(1417780800);
It will return Date in String format.
Jan 17, 1970
I'm having a time zone issue with my SimpleDateFormat. Here is my code:
TextView date_time = (TextView) view.findViewById(R.id.date_time);
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date = null;
try {
date = df.parse(workout.getDate().toString());
System.err.println("position: " + position + " workout: " + workout.getDate().toString());
System.err.println("position: " + position + " date: " + date);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat("EEEE, MMMMM dd", Locale.US);
date_time.setText(df.format(date));
Note this output:
position: 0 workout: Mon Aug 19 00:00:00 MDT 2013
position: 0 date: Sun Aug 18 23:00:00 MDT 2013
Do you see how the string starts (correctly) with the date being Aug 19 (at midnight). But then after the date formatter does its work, I come out with the time being 1 hour earlier. I'm assuming that this is some time zone manipulation, but I don't know how to correct for it. I tried some different values for 'Z' (including 'Z', 'ZZZ', 'ZZZZ', and 'ZZZZZ'), but all give the same result. I assume that it's a time zone problem, but in both cases is shows 'MDT', so maybe not.
How do I stop this one hour shift from happening? Thanks!
EDIT: VERY hacky solution but it works:
try {
date = df.parse(workout.getDate().toString());
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, 1);
date = cal.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
I am trying to convert given date and time to milliseconds. I am not able to achieve it.
Below is the format of date and time
02 - 07 (DD - MM)
08:50:00 AM (hh mm ss a)
Here is what I have tried:
String myDate = "2-05";
String myTime = "08:50:00";
String ampm = "AM";
String toParse = myDate + " " + myTime+" " + ampm;
try
{
SimpleDateFormat formatter3 = new SimpleDateFormat("d-M hh:mm:ss a"); // I assume d-M, you may refer to M-d for month-day instead.
Date date;
date = formatter3.parse(toParse);
long millis = date.getTime();
Log.e ("Date in milli",""+millis);
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} // You will need try/cat
Below is the error I am getting:
08-04 16:50:34.368: W/System.err(6603): java.text.ParseException: Unparseable date: "2-05 08:50 AM"
08-04 16:50:34.368: W/System.err(6603): at java.text.DateFormat.parse(DateFormat.java:626)
Not sure where I am going wrong? Can somebody help me out ?
Thanks!
There is a difference between the exception you're getting and the date you're supplying in the code. The exception says the provided string is 2-05 08:50 AM, but you should be providing the seconds field as well. It should be 2-05 08:50:00 AM is there a mismatch somewhere? Are you running old code?
As others have identified, if your month is always two digits, you need to use MM instead of M