Date String to Milliseconds [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
I have a time string like 4:52 pm. I want to convert it in milliseconds for comparison with current time of system. How is it possible in android ?

long millis = 0;
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
try {
Date d = dateFormat.parse("4:52 PM");
millis = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}

String someTime = "4:52 pm";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
Date date = sdf.parse(someTime);
System.out.println(date.getTime());

Related

i'm getting 12:00am instead of 12:00pm in android studio what should i do? [duplicate]

This question already has answers here:
12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")
(1 answer)
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
Closed 1 year ago.
public static String getTime(String time) {
String ampmTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
Date dt;
try {
dt = sdf.parse(time);
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a", Locale.getDefault());
if (dt != null) {
ampmTime = sdfs.format(dt);
}
} catch (ParseException e) {
e.printStackTrace();
}
return ampmTime;
}
In parameter string i'm sending 12:00:00 format so please i want this as 12:00pm instead of 12:00am . And others time is showing correctly like 13:30:00 to 1:30pm . So please help me??
As stated here, When using hh you are telling the formatter the input time is an am/pm time. Use HH for 24hrs input.
Change the format to:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());

simple date format giving wrong month [duplicate]

This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 5 years ago.
I was trying to convert date from yyyy-MM-dd format to yyyy-MM format, when I run the below code with the input "2012-10-22" it is giving me an output of 2012-JAN instead of 2012-OCT. any thoughts on where I am doing wrong?
public static String dateFormatter(String presentDate)
{
String formattedDate = "";
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat finalFormat = new SimpleDateFormat("YYYY-MMM");
try {
Date currentFormat = tempFormat.parse(presentDate);
formattedDate = finalFormat.format(currentFormat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formattedDate;
}
Change the first format to
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
as DD is the day in the year. 22 is definitely in January
Use this
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
Instead of
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");

Android cannot compare dates with yyyy-MM-dd format [duplicate]

This question already has answers here:
Java Unparseable Date Exception
(3 answers)
Closed 7 years ago.
Android comparing date with yyyy/MM/dd format only force closes when compared with yyyy-MM-dd format.
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
String d = "2016/12/29";
expiryDate = (Date) formatter.parse(d);
I hope this one help you.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date finalStartTime = format.parse(YOUR_OLD_DATE);
Date finalEndTime = format.parse(YOUR_NEW_DATE);
if (finalEndTime.after(finalStartTime)) {
//your code
}
} catch (ParseException e) {
}
If still not worked let me know...
Just Change "-" to "/" :
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd",
Locale.getDefault());
String d = "2016/12/29";
try {
expiryDate = (Date) formatter.parse(d);
} catch (ParseException e) {
//do something
}
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String d = "2016/12/29";
Date expiryDate = formatter.parse(d);

Add days to date format MM/dd/yyyy in selenium webdriver [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 7 years ago.
The question is simple, how to add days to this date format:
MM/dd/yyyy like 11/06/2015
You can do something like this:
String format = "mm/dd/yyyy";
String date = "11/06/2015";
SimpleDateFormat simpleFormat = new SimpleDateFormat("mm/dd/yyyy");
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
java.util.Calendar calendar = java.util.Calendar.getInstance();
try {
calendar.setTime(df.parse(date));
calendar.add(java.util.Calendar.DAY_OF_MONTH, +1);
String formatted = simpleFormat.format(calendar.getTime());
System.out.println(formatted);
} catch (Exception ex) {
System.out.println("Error: " + ex.toString());
}

How to validate a date in Java? [duplicate]

This question already has answers here:
How to compare dates in Java? [duplicate]
(11 answers)
Closed 7 years ago.
I have a date coming in this format -
2015-4-10T11:20:56
I need to validate the date and make sure date it is not greater than current date. Meaning if today is April 10th, then it should not be April 11th or greater than that.
String abc = "2015-4-10T11:20:56";
if(abc is greater than todays date) {
// log an error
}
How can I do this?
UPDATE:-
I tried parssing like this but it didn't worked -
String abc = "2015-4-10T11:20:56";
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
try {
format.parse(abc);
} catch (ParseException e) {
e.printStackTrace();
}
You have to convert the string to a date object.
You can use a SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d'T'HH:mm:ss");
Date date = sdf.parse(dateStr);
if (date.compareTo(new Date()) > 0) {
// log an error
}
This should work for you:
String abc = "2015-4-10T11:20:56";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
Date mydate = df.parse(abc);
Calendar c = Calendar.getInstance();
c.setTime(mydate);
Calendar today = Calendar.getInstance();
if (c.compareTo(today)>=0){
}
You can try like this using compareTo
Date date = null;
String str = "2015-4-10T11:20:56";
try {
DateFormat f = new SimpleDateFormat("yyyy-M-d'T'HH:mm:ss");
f.setLenient(false);
date = f.parse(str);
if (date.compareTo(new Date()) > 0) {
// your code
}
} catch (ParseException e) {
e.printStackTrace();
}

Categories