This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 2 years ago.
How to convert newyork timezone time to "Asia/Kolkata" date and time?
my Input String:
11/28/2012 8:59am
My code:
String dtStart = "11/28/2012 8:59am";
SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyyy hh:mma");
format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
try {
Date date = format.parse(dtStart);
Log.i("clock", date.toString());
System.out.println(date);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(date.getTime());
cal.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
int chour = cal.get(Calendar.HOUR_OF_DAY);
int cminute = cal.get(Calendar.MINUTE);
int dd = cal.get(Calendar.DAY_OF_MONTH);
int mm =cal.get(Calendar.MONTH);
int yy =cal.get(Calendar.YEAR);
String mode="AM";
if(chour>12)
{
chour=chour-12;
mode="PM";
}
String mytime=Integer.toString(chour)+":"+Integer.toString(cminute)+" "+mode;
String mydate=Integer.toString(dd)+"/"+Integer.toString(mm)+"/"+Integer.toString(yy);
Log.i("clock", mytime);
Log.i("clock", mydate);
} catch (ParseException e) {
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
My output Log:
Sat Jan 28 19:29:00 GMT+05:30 2012
7:29 PM
28/0/2012
Here my time is correct,but date is wrong. I expect 28/11/2012.I am not able trace where i did wrong?
the issue is in format you entered: "mm/dd/yyyy hh:mma". I believe you wanted to do this: "MM/dd/yyyy hh:mma"
SimpleDateFormat formatter = new SimpleDateFormat("MM/DD/YYYY hh:mm:ss a");
String dtStart = "11/28/2012 08:59 am";
Date dateObj = formatter.parse(dtStart);
cal.setTime(dateObj);
fromTZObj = TimeZone.getTimeZone("America/New_York");
toTZObj = TimeZone.getTimeZone("Asia/Kolkata");
Calendar fromCal = new GregorianCalendar(fromTZObj);
fromCal.set(cal.get(1), cal.get(2), cal.get(5), cal.get(11), cal.get(12),
cal.get(13));
Calendar toCal = new GregorianCalendar(toTZObj);
toCal.setTimeInMillis(fromCal.getTimeInMillis());
Date dd = toCal.getTime();
formatter.setTimeZone(toTZObj);
String formattedDate = format.format(dd);
System.out.println(formattedDate);
Related
This question already has answers here:
java.text.ParseException: Unparseable date
(10 answers)
Closed 3 years ago.
I'm attempting to parse a new Date object, but I keep hitting the following error.
W/System.err: java.text.ParseException: Unparseable date: "Thu May 16 09:28:39 GMT+01:00 2019"
I've attempted various different patterns for dateFormat, but nothing seems to work.
This is where the error is.
c.setTime(dateFormat.parse(oldDate));
Code
public static String addDay(int numberOfDays) {
String oldDate = String.valueOf(new Date());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
try {
c.setTime(dateFormat.parse(oldDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}
Try This function
In your question you are converting Date to string
then after you are once again Parsing String to Date
that is very Long way. you can directly set as
c.setTime(oldDate);
public static String addDay(int numberOfDays) {
Date oldDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
c.setTime(oldDate);
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}
The pattern should be like :
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
Then to print you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
Your pattern is wrong. You should use EEE MMM dd HH:mm:ss z yyyy
How can I convert 24 hours time format into 12 hours format? I know this question has been asked many times, but my problem is different. My current time is:
Tue Nov 07 18:44:47 GMT+05:00 2017
I just want 6:44 pm from my date time. I tried this:
private void convertTime(String time)
{
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
}
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Using hh will give you 12 hours format and HH 24 hour format. More details on documentation.
Edit:
Your initial format must be the following in order to parse your date string to a Date object:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'Z yyyy");
final Date dateObj = sdf.parse(time);
After that you can format time to your needs.
From SimpleDateFormat documentation:
"h:mm a": 12:08 PM
So the format you need for:
I just want 6:44 pm from my date time
is:
final SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date date = sdf1.parse(time);
final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String newDateString = sdf.format(date);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
This question already has answers here:
java date format yyyy-mm-dd.hh.MM.ss.ms
(3 answers)
Closed 7 years ago.
I have a date string which is as follows:
2015-12-24 08:06:44
Now I have a parse function:
public static String parseDate(String date) {
Date oldDate = null;
try {
System.out.println("Unparsed: "+date);
DateFormat oldFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
oldDate = oldFormat.parse(date);
System.out.println("old Date parsed: "+oldDate);
DateFormat newFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);
Date result = newFormat.parse(oldDate.toString());
return result.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
The first System.out gives me:
Unparsed: 2015-12-24 08:06:44
The second one:
Sat Jan 24 08:06:44 GMT+05:30 2015
Whereas for the second one I have clearly mentioned the date format to be as:
new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Please advise on what's wrong here
I only want to get the month and day from the input string - Desired would be Dec 24 from the above sample date string.
Its MM for month not mm.
DateFormat oldFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
SimpleDateFormat doc
Let us suppose we have a date show as.
2015-08-03 12:00:00
How would I convert that to a day's name like Tuesday ? I don't want things like 03 Tue etc. Just the full days name. I looked around but I am bit confused on that.
First, parse that date into a java.util.Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yourDate = formatter.parse("2015-08-03 12:00:00");
Then, populate a Calendar with this date:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Now you have your day of week dayOfWeek (1 being SUNDAY, for example).
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = simpleDateformat.parse("2015-08-03 12:00:00");
simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely
System.out.println(simpleDateformat.format(now));
This is the solution I came up with:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
Date weekDay = null;
try {
weekDay = formatter.parse("2015-08-03 12:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat outFormat = new SimpleDateFormat("EEEE");
String day = outFormat.format(weekDay);
I have a string "2014-07-02T17:12:36.488-01:00" which shows the Mountain time zone. I parsed this into java.util.date format. Now I need to convert this to GMT format. Can anyone help me??
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Object dd = null;
try {
dd=sdf.parseObject("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();`enter code here`
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:+ gmtDateFormat.format(dd));
There are a few problems in your code. For example, the format string doesn't match the actual format of the string you are parsing.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Object dd = null;
try {
dd = sdf.parse("2014-07-02T17:12:36.488-01:00");
System.out.println(dd);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX");
gmtDateFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
System.out.println("Current Date and Time in GMT time zone:" + gmtDateFormat.format(dd));
To print the current date in whatever timezone you like, set the timezone you want to use on the SimpleDateFormat object. For example:
// Create a Date object set to the current date and time
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(now));
df.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println("Current date and time in IST: " + df.format(now));