Getting the month and day from a TimeStamp - java

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);

Related

how to convert GMT to Local date time in android

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();
}

Date format in java

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.

"Unparsable date" when trying to reformat a date String

I want to reformat a given date String into a different format:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
I'm getting this exception:
java.text.ParseException: Unparseable date: "Monday, December 7, 2015 at 12:05:13 PM Eastern Standard Time" (at offset 33)
You missed the seconds from your date format.
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm:ss aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
Edit. For the following example: Thursday 12/10/2015 01:35 AM, the date format is this:
SimpleDateFormat output = new SimpleDateFormat("EEEE dd/MM/yyyy hh:mm a");

Convert Current Timestamp to DD MMM YYYY format

I am trying to convert current timestamp to DD MMM YYYY format.
but i don't know why it is giving me an error unparsable.
code:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
System.out.println(format.format(fechaNueva));
working but I want to do
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
gives me error of unparsable.
java.text.ParseException: Unparseable date: "2014-10-07 15:38:29.876"
Following worked for me
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(fechaNueva));
Output:
07 Oct 2014, 15:46
You were trying to parse date in a format you wanted it to be formatted instead of format in which it is already present.
Simply u can do this
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println("Formatted "+dateFormat.format(timestamp));
hope this helps.
OutPut:-
Formatted 07 Oct 2014, 15:59
You can't use ts.toString(), you always need to use the same formatter.
Use just:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(ts));

Convert string to date Android

I'm trying to convert a String that represents a date stored in SQLITE.
The date was stored into sqlite as follows:
Date date;
date.toString();
According with Java documentation, toString() method:
Returns a string representation of this Date. The formatting is
equivalent to using a SimpleDateFormat with the format string "EEE MMM
dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00
PDT 1999". The current default time zone and locale are used. If you
need control over the time zone or locale, use SimpleDateFormat
instead.
Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.
The String comes from sqlite:
Mon Jan 20 18:26:25 BRT 2014
So, I do:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");
What I'm doing wrong?
Thanks.
try this code
String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Try this:
String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = pre.parse(w);
System.out.println(sdf.format(date));
}catch(Exception e){
e.printStackTrace();
}
Output:
20/01/2014
Formatter for storing and restoring data value in format dd/MM/yyyy
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Storing data
String dataAsString = simpleDateFormat.format(date); // 20/01/2014
Restoring data
Date data = simpleDateFormat.parse(dataAsString);

Categories