I have the following code snippet.
#Override
public String toString() {
try {
String calString = "Sat Sep 27 00:00:00 EDT 2014";
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z YYYY");
System.out.println("calString " + calString);
Date date = formatter.parse(calString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("month " + calendar.get(Calendar.MONTH));
System.out.println("day " + calendar.get(Calendar.DAY_OF_MONTH));
DateFormat df = new SimpleDateFormat("YYYYMMdd");
System.out.println("format date" + df.format(date));
return df.format(date);
} catch (ParseException ex) {
return null;
}
}
Expected output should be 20140927
but I'm getting this instead.
calString Sat Sep 27 00:00:00 EDT 2014
month 0
day 3
format date 20140103
Anybody know why the day and month are off?
Y represents week year. y is used to match the year
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
You're using YYYY instead of yyyy. That means "week year", to be used in conjunction with "week of year". Just change YYYY to yyyy in both of your SimpleDateFormat constructors and you'll get output of:
calString Sat Sep 27 00:00:00 EDT 2014
month 8
day 27
format date20140927
Note that month is 8 rather than 9 because months in java.util.Calendar are 0-based.
A little more healthy solution
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"
,Locale.ENGLISH);
Related
I'm trying to convert system local date to UTC. Below is my code and it looks working for MST and EST formats. But, it is not working as expected.
String inputDate = "Wed Apr 13 04:00:00 IST 2022";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = sdf.parse(inputDate);
DateFormat formatUTC = new SimpleDateFormat("MM/dd/yyyy");
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
String result = formatUTC.format(date);
System.out.print(result); // 04/13/2022
I see that IST zone is 5hrs 30mins ahead from the UTC universal time. So, I should get 04/12/2022 for the given input. But, getting 04/13/2022. what am I doing wrong here? Please advise.
Try setting the timezone for inputDate as well. Try the below code:
String inputDate = "Wed Apr 13 04:00:00 IST 2022";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
Date date = sdf.parse(inputDate);
DateFormat formatUTC = new SimpleDateFormat("MM/dd/yyyy");
formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
String result = formatUTC.format(date);
System.out.print(result);
Take a look at this answer.
I have a time like this : Thu, 27 Oct 2016 18:17:47 GMT
I have tried the code bellow but didn't work
String inputText = "Thu, 27 Oct 2016 18:17:47 GMT";
SimpleDateFormat inputFormat = new SimpleDateFormat
("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("MMM dd, yyyy h:mm a");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
Your input date and input format don't match each other. Try this input format instead:
SimpleDateFormat inputFormat = new SimpleDateFormat
("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
Here are a couple of functions that will do it:
public static Date toLocale(Date date) {
int GMTOffset = (int) TimeUnit.HOURS.convert(calendar.getTimeZone().getRawOffset(),
TimeUnit.MILLISECONDS);
return addHours(date, GMTOffset);
}
public static Date addHours(Date date, int hours) {
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
Am getting response as
"pickTime": "Tue Oct 25 03:57 PM"
i had coded as follows to get.
SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a");
SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");
String time = jsonObj.optString("pickTime");
Date dateObj = curFormater.parse(time);
String newDateStr = dateFormater.format(dateObj);
String newTimeStr = timeFormater.format(dateObj);
The problem is I am getting same value in String time but in dateobj I am getting value as
"sun oct 25 15.57 pm"
newDatestr as
"sun oct 25"
Please help me to solve this problem ,thanks in advance
It is taking the October 25 of 1970 (Linux time).
You have to figure out how to add the year to the date because it is not taking the current year.
It takes the default one instead (the linux time starts on January 1 1970).
Your response does not contains year. So you are getting incorrect output.
Below code is working try it out.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a yyyy");
SimpleDateFormat dateFormater = new SimpleDateFormat("E MMM dd");
SimpleDateFormat timeFormater = new SimpleDateFormat("hh:mm a");
String time = "Tue Oct 25 03:57 PM" + " " + calendar.get(Calendar.YEAR);
Date dateObj = null;
try {
dateObj = curFormater.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Logger.logd(this, dateObj.toString());
String newDateStr = dateFormater.format(dateObj);
String newTimeStr = timeFormater.format(dateObj);
Logger.logd(this, newDateStr + " " + newTimeStr);
SimpleDateFormat formatter =new SimpleDateFormat("EEE MMM dd hh:mm a");
Date date2 = formatter.parse("Tue Oct 25 03:57 PM");
long time = date2.getTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.YEAR, 2016);
String formatted = formatter.format(cal.getTime());
System.out.println(formatted);
#David Marciel is right. you should specify year.
SimpleDateFormat curFormater = new SimpleDateFormat("E MMM dd hh:mm a");
in this why you have put E.
SimpleDateFormat curFormater = new SimpleDateFormat("EEE MM dd yyyy hh:mm a");
I think this should be format.
I am receiving a Java Date formatted like so: "Sun Sep 14 02:00:00 PDT 2014" into a yyyy-MM-dd format but I can't seem to parse it. What I tried is the following:
String time = "Sun Sep 14 02:00:00 PDT 2014";
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date contractEffectiveDateFormat = f.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
However, I get an error saying that this date is unparsable. I'm not sure how to go about parsing this date because if I try to parse the date using the following:
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
which is how to actually parse the date correctly into a Date object,
the string would turn into a Date object, but I can't seem to do anything with it from there. I want to turn it in so that it looks like 2014-09-14. Any ideas on how to do so? Thanks!
Use two DateFormat(s) one for input and for output,
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
DateFormat in = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
try {
Date effectiveDate = in.parse(time);
System.out.println("Date: " + out.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Output is the requested
Date: 2014-09-14
Your incoming string is this String time = "Sun Sep 14 02:00:00 PDT 2014";
which means the SimpleDateFormat pattern should match the incoming String pattern so you need to use SimpleDateFormat like this
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
Then when you called parse() on inFormat it will give you Date Object which doesnot have particular format associated with it. So in order to format the Date again you need to create SimpleDateFormat object specifying the format you want which is this
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Ultimately bind all together
One more thing always specify the Locale
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// good practice to specify the locale
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
try {
Date parsedDate = inFormat.parse(time);
System.out.println("Required Formatted Date: " + outFormat.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Simply add another SimpleDateFormat that'll allow you to present the Date object the way you want:
public static void main(String[] args) throws ParseException {
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
Date contractEffectiveDateFormat = df.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(f.format(contractEffectiveDateFormat)); // prints 2014-09-14
}
I have a String timeStamp in this format "Wed, 29 Jan 2014 20:14:15 GMT". I want to be able to compare this date with another date that is in this format 01/25/1999. I have tried simpledateformatter but with no success.
String a = connection.getHeaderField("Last-Modified"); //Returns Wed, 29 Jan 2014 20:14:15 GMT
Date lastModDate = new Date(file.lastModified()); //returns 01/25/1999
This is the simpleDateFormatter I tried implementing
SimpleDateFormat formatter;
Date dateIn = null;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try{
dateIn = (Date)formatter.parse(a);
}catch(ParseException e){
e.printStackTrace();
}
Log.d(TAG, "The server date is formated to : " + dateIn);
The dateIn is always null.
I want the to be able to do something like this
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
}
Use the following code...you will get the problem right.
Calendar calender = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String timeStamp = formatter.format(calender.getTime());
Date strDate = formatter.parse(timeStamp);
String currentTimeStamp = formatter.format(new Date());
Date currentTime = formatter.parse(currentTimeStamp);
if (currentTime.after(strDate)) {
}
Don't know what you tried but this should work:
String a = connection.getHeaderField("Last-Modified");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(a);
This can help http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The reason is that you are using the wrong date format for your formatter. If the date you receive looks like
"Wed, 29 Jan 2014 20:14:15 GMT"
Then you should use the following format
formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
You should use the Calendar class and its subclass GregorianCalendar. For exampe, to get the month of your date:
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.get(Calendar.MONTH);