String to timestamp Android - java

I have the following string:
19 July 2016 at 07:00:00 UTC
and am using the below code to convert it to a timestamp. However the console is throwing the following error:
java.text.ParseException: Unparseable date: "19 July 2016 at 07:00:00 UTC" (at offset 2)
CODE:
String time = todayAt7;
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
try {
Date date = sdf.parse(time);
System.out.println("Date and Time: " + date);
} catch (Exception e1) {
e1.printStackTrace();
}
Essentially I am trying to figure out the timestamp for 'today at 7am', is there a way to do this that I am missing?

In your code DateFormat sdf = new SimpleDateFormat("hh:mm:ss"); is not correct. The string argument should match the format of the date string your are trying to parse
try below
DateFormat sdf = new SimpleDateFormat("dd mmmm yyyy 'at' HH:mm:ss z");

If you want timestamp at today 7 AM, you can try the below
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 7);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(cal.getTime());
Output:
Tue Jul 19 07:00:00 EDT 2016

Related

How to set datetime to end of day in java

I want to set datetime of day as: startDate=2018/03/28 00:00:00 and endDate=2018/03/28 23:59:59
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
String str1=sdf1.format(cal.getTime());
String str2=sdf2.format(cal.getTime());
Date startDate = sdf1.parse(str1);
Date endDate = sdf2.parse(str2);
My problem:program is working and output endDate=2018/03/28 00:00:00
Would you please point out any mistakes to me in code?
update:
i used debug and it's working correct with
String str2=sdf2.format(cal.getTime());//2018-03-28 23:59:59
but when change string==>date is not correct with output 2018/03/28 00:00:00
If you want to initialize Date instances from a formatted string with both date and time then time codes should be added to the SimpleDateFormat pattern to parse strings in that format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse("2018-03-28 00:00:00");
Date endDate = sdf.parse("2018-03-28 23:59:59");
If you want to simply set the hour, minute, and second on the current date then use a Calendar instance and set fields on it accordingly.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0)
Date startDate = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999)
Date endDate = cal.getTime();
And next output the Date in a particular format:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(sdf.format(startDate));
System.out.println(sdf.format(endDate));
Output:
2018/03/28 00:00:00
2018/03/28 23:59:59
Dealing with time zones
If time zone is other than the local time zone then it's a good idea to be explicit with what timezone you're working with. Calendar and SimpleDateFormat instances must be consistent with what timezone you're dealing with or the date and/or times may be off.
TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar cal = Calendar.getInstance(utc);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
sdf.setTimeZone(utc);
A substitute for SimpleDateFormat is using DateTimeFormatter class found in the newer java.time package added to Java 8.
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str1=sdf1.format(cal.getTime());
String str2=sdf2.format(cal.getTime());
try {
Date startDate = sdf3.parse(str1);
Date endDate = sdf3.parse(str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(startDate.toString());
System.out.println(endDate.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OUT PUT
2018-03-28 00:00:00
2018-03-28 23:59:59
Wed Mar 28 00:00:00 ICT 2018
Wed Mar 28 23:59:59 ICT 2018
I think, maybe sdf1 and sdf2 don't provide clear format.
So change time to HH:mm:ss.
If you just want to get the Date values for today's start and end times, you don't need to use date formatting utilities (like SimpleDateFormat) at all:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
Date startDate = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
Date endDate = cal.getTime();
You can solve this problem like this
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
String str1=sdf1.format(cal.getTime());
String str2=sdf2.format(cal.getTime());
Date startDate = sdf1.parse(str1);
Date endDate = sdf2.parse(str2);
String startDateTime = sdf1.format(startDate);
String endDateTime = sdf2.format(endDate);
System.out.println("startDate ----->" + startDateTime);
System.out.println("endDate ----->" + endDateTime);
The output of this
startDate ----->2018-03-28 00:00:00
endDate ----->2018-03-28 23:59:59
Hope this is what you want.

SimpleDateFormat returning wrong day

I am trying to convert the date from May 15, 2009 19:24:11 PM MDT to 20090515192411.
But when I tried the below code, the readformat itself is taking the input as May 16 instead of May 15
Here is my code.
String dateInString = "May 15, 2009 19:24:11 PM MDT";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss a z");
DateFormat writeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = null;
try {
date = readFormat.parse(dateInString);
}
catch(ParseException e) {
e.printStackTrace();
}
System.out.println(date); // Prints May 16, 2009 07:24:11 AM MDT
String formattedDate = "";
if( date != null ) {
formattedDate = writeFormat.format(date);
}
System.out.println(formattedDate); // Prints 20090516072411
Thanks for the help in advance.
String dateInString = "May 15, 2009 19:24:11 PM MDT";
is invalid, time could be either 24 hour format or it could have AM/PM
You need HH instead of hh to read a time in 24-hour format. Java's "lenient dates" are doing you in here - 19:24pm is being parsed as 8 hours after 11:24pm.

Possible to remove the time and just want the date from getTime() Date class?

Is it possible to remove the day (Fri), the time (22:34:21) and the time zone (GMT) by just having an output like "Jan 11 1980" instead of "Fri Jan 11 22:34:21 GMT 1980"??
Code below:
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 1980);
date.set(Calendar.MONTH, 0);
date.set(Calendar.DAY_OF_MONTH, 11);
Date dob = date.getTime();
System.out.println(dob);//Fri Jan 11 22:34:21 GMT 1980
Many thanks!
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(date));
Output:
Feb 26 2013
If you want a specific date, do
Calendar c = Calendar.getInstance();
c.set(1980, 0, 11);
Date date = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(date));
Prints
Jan 11 1980
public class DateFormat {
public static void main(String[] args) {
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 1980);
date.set(Calendar.MONTH, 0);
date.set(Calendar.DAY_OF_MONTH, 11);
Date dob = date.getTime();
System.out.println(new SimpleDateFormat("MMM dd yyyy").format(dob));
}
}
Output:
Jan 11 1980
Date is a representation of the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT)
In order to "remove" the time portion of a Date, you will want to use a DateFormat
Something as simple as;
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(dob));
Should work.
For a more localised version, you should use DateFormat.getDateInstance()
System.out.println(DateFormat.getDateInstance().format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM).format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(dob));
DateFormat dateFormatter = DateFormat.getDateInstance();
System.out.println(dateFormatter.format(date);
This will print the only the date corresponding to your current system locale settings.
See also: DateFormat in the JavaDoc
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(dob));
you can use:
stringToPrint = time.getMonth()+" "+time.getDate()+" "+time.getYear();
for more info:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html

Date / time changed to current server time in java calendar when adding seconds

I create a calendar object from a string date I receive.
Calendar cal = Calendar.getInstance();
String date = "example Mon, 22 Oct 2012 23:58:31 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
sdf.setTimeZone (TimeZone.getTimeZone ("PST"));
try {
cal.setTime(sdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("date "+sdf.format(cal.getTime()));
This date converts and prints the gmt time to pst with no problem.
I then loop through an array of the elements object and add seconds
int offset = element.getSeconds;
cal.add(Calendar.SECOND, offset);
System.out.println("times cal offset: " + cal.getTime());
This cal now prints the server time (which is eastern time) and not the converted gmt that is printed above. Does something bypass the cal created from the string gmt date when seconds are added?
Thanks!
This because you don't use SDF with setted GMT timezone for printing in second time.

Simpledateformat ParseException

I need to change the input date format to my desired format.
String time = "Fri, 02 Nov 2012 11:58 pm CET";
SimpleDateFormat displayFormat =
new SimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa z");
Date date = parseFormat.parse(time);
System.out.println("output is " + displayFormat.format(date));
it gives me this error
java.text.ParseException: Unparseable date: "Fri, 02 Nov 2012 11:58 pm CET"
at java.text.DateFormat.parse(Unknown Source)
at Main.main(Main.java:10)
Can anyody help me? Because this code doesn't work.
It appears Android's z does not accept time zones in the format XXX (such as "CET"). (Pulling from the SimpleDateFormat documentation.)
Try this instead:
String time = "Fri, 02 Nov 2012 11:58 pm +0100"; // CET = +1hr = +0100
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aa Z"); // Capital Z
Date date = parseFormat.parse(time);
SimpleDateFormat displayFormat =
new SimpleDateFormat("dd.MM.yyyy, HH:mm");
System.out.println("output is " + displayFormat.format(date));
output is 02.11.2012, 22:58
Note: Also, I think you meant hh instead of HH, since you have PM.
Result is shown here. (This uses Java7's SimpleDateFormat, but Android should support RFC 822 timezones (+0100) as well.)
NB: Also, as it appears Android's z accepts full names ("Pacific Standard Time" is the example they give), you could simply specify "Centural European Time" instead of "CET".
Try out the following code:
SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMMdd");
System.out.println(date_format.format(cal.getTime()));
It will work.. If not print the log cat? What erroe is coming?
First of All I must agree with #Eric answer.
You just need to remove "CET" from your string of date.
Here is sample code. Check it.
String time = "Fri, 02 Nov 2012 11:58 pm CET";
time = time.replaceAll("CET", "").trim();
SimpleDateFormat displayFormat =
new SimpleDateFormat("dd.MM.yyyy, HH:mm");
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm aa");
Date date = null;
try {
date = parseFormat.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("output is " + displayFormat.format(date));

Categories

Resources