Adding time to Date Object - java

I am trying to add time to date object. I am getting date in dd MMM yyyy format and time in h:mm a format. How can i combine both of them into single date object.
public static String getDateAsString(String formattedDate, String timeTaken) {
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy h:mm a");
try {
return "/Date(" + Long.toString(format.parse(formattedDate).getTime()) + ")/";
} catch (ParseException e) {
return "";
}
}
I want to form and send the date in below format to Web-Service.
String date = "/Date(12323232323232332)/";

Try changing this
format.parse(formattedDate)
To this
format.parse(formattedDate + " " + timeTaken)

Try this, i have not tested it locally but see if it works:
public static String getDateAsString(String formattedDate, String timeTaken) {
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy h:mm a");
try {
return format.parse(formattedDate + " " + timeTaken);
} catch (ParseException e) {
return "";
}
}

Related

how to convert multiple format of dates into only one format using java

from the database postgressql i'm getting date sometime date in different different forlamts like "2015-11-26 09:30:10","2015-11-26 09:30:10.080","2015-11-26 09:30:10.000" now i want to convert all format as only this format 22/01/2018 how to do in java.
try {
Date theDate1 = new Date("JAN 13,2014 09:15");
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
} catch (Exception e {
System.out.println(e.toString());
}
i got output as Hello, World !13 / 01 / 2014 but when i tried
try {
Date theDate1 = new Date("2017-11-27 00:00:00");
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
} catch (Exception e) {
System.out.println(e.toString());
}
then i got java.lang.IllegalArgumentExceptionhow to solve this problem
Try this
String text = "JAN 13,2014 09:15";
String patternst = "\\d\\d\\d\\d-\\d\\d-\\d\\d*";
Pattern pattern = Pattern.compile(patternst);
Matcher matcher = pattern.matcher(text);
String data = "";
while (matcher.find()) {
data = matcher.group(0);
}
try {
int year = Integer.parseInt(data.split("-")[0]);
int month = Integer.parseInt(data.split("-")[1]);
int day = Integer.parseInt(data.split("-")[2]);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
Date theDate1 = cal.getTime();
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
} catch (Exception e) {
Date theDate1 = new Date(text);
SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
String temp = format1.format(theDate1);
System.out.println("Hello, World! " + temp);
}

How to compare time exist between two times in android

String time1="09-00 AM"; String time2="07-00 PM";
String date= 15-09-2017 04:04:33
String time1 = "09-00 AM";
String time2 = "07-00 PM";
public static void main(String[] args) {
try {
DateFormat df = new SimpleDateFormat("HH:mm:SS");
Date time1 = df.parse("09-00 AM");
Date time2 = df.parse("07-00 PM");
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sf.format(date);
TimeZone tz1 = TimeZone.getTimeZone("PST");
Date c = shiftTimeZone(date, tz1);
System.out.println("Format : " + new SimpleDateFormat("HH:mm:SS").format(c));
System.out.println(time1 + "" + time2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
How can I compare that date time is exist between time1 and
time2 please suggest how I can check this, I am not able to split time or merge with date.
For that I am trying to Convert time1 and time2 in Date object in HH:mm:SS
Use .after method to compare two dates
check this link for more details
public static void main(String[] args) {
try {
SimpleDateFormat df = new SimpleDateFormat("HH:mm a");
Date time1 = df.parse("09-00 AM");
Date time2 = df.parse("07-00 PM");
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//converts date to required format
String current = sf.format(date);
Date currentTime = df.parse(current);
// TODO COMPARE currentTime with time1 or time2 as per your need
System.out.println(time1 + "" + time2 +" " + currentTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

My Logic is working fine, but seems too vague.Is there any other easy way to convert "Aug/2016" to "08/2016" in java?

Is there any other easy way to convert "Aug/2016" to "08/2016" in java?
My Logic is working fine, but seems too vague.
String myDate = "Aug/2016";
String str = myDate.split("/")[0];
Date date;
try {
date = new SimpleDateFormat("MMMM").parse(str);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
System.out.println("##### ----- month in number : " +cal.get(Calendar.MONTH+1));
int year = Calendar.getInstance().get(Calendar.YEAR);
int mnth = cal.get(Calendar.MONTH) + 1;
str = String.valueOf(mnth+"/"+year);
System.out.println("##### ----- new selected date : " +str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
suggestions please!...
Simply use SimpleDateFormat:
String myDate = "Aug/2016";
SimpleDateFormat parser = new SimpleDateFormat("MMM/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/yyyy");
System.out.println(formatter.format(parser.parse(myDate)));

Adding 1 hr dynamically in android

I have a code
String date = 05/09/13 10.55 PM;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;
testDate = sdf.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..." + newFormat);
And this gives me output as
05/09/13 10:55:00 PM
What i actually need:
05/09/13 11:55:00 PM //i want to add an hour to the date I got
Use below code This will add 1 hour and print required result.
String date = "05/09/13 10.55 PM";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = null;
try {
testDate = sdf.parse(date);
// Add 1 hour logic
Calendar tmpCalendar = new GregorianCalendar();
tmpCalendar.setTime(testDate);
tmpCalendar.add(Calendar.HOUR_OF_DAY, 1);
testDate = tmpCalendar.getTime();
// Continue with your logic
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..." + newFormat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output
.....Date...05/09/2013 11:55:00 PM
Date newDate = DateUtils.addHours(testDate, 1);
DateUtils.addHours
Edit:
Here u are:
String date = 05/09/13 10.55 PM;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh.mm a");
Date testDate = sdf.parse(date);
Date newDate = DateUtils.addHours(testDate, 1);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String newFormat = formatter.format(newDate);
System.out.println(".....Date..." + newFormat);

Java: Date Time Conversion

I have a date format like "SA25MAY"; I need to convert it into date time variable and then I want to add one day in that. And then I need to return the answer in same format. Please do some needful
try {
String str_date = "SA25MAY";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("ddd-dd-MMM");
date = (Date) formatter.parse(str_date);
System.out.println("Today is " + date);
} catch (Exception e) {
e.printStackTrace();
}
ERROR:
java.text.ParseException: Unparseable date: "SA25MAY"
at java.text.DateFormat.parse(DateFormat.java:337)
at javadatatable.JavaDataTable.main(JavaDataTable.java:29)
Here I don't know how to resolve this problem.
ddd can not match SUN. Use EEE instead if you want to match the day name in the week.
You can only add one day if you know the year because of leap years (29th of February).
In case the year is the current year, the following solution should do the the job:
For "SA25MAY":
try {
String str_date = "SA25MAY";
// remove SA
str_date = str_date.replaceFirst("..", "");
// add current year
Calendar c = Calendar.getInstance();
str_date = c.get(Calendar.YEAR) + str_date;
// parse date
Date date;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyddMMM");
date = formatter.parse(str_date);
System.out.println("Today is " + date);
// add day
c.setTime(date);
c.add(Calendar.DATE, 1);
// rebuild the old pattern with the new date
SimpleDateFormat formatter2 = new SimpleDateFormat("EEEddMMM");
String tomorrow = formatter2.format(c.getTime());
tomorrow = tomorrow.toUpperCase();
tomorrow = tomorrow.substring(0, 2) + tomorrow.substring(3);
System.out.println("Tomorrow is " + tomorrow);
} catch (Exception e) {
e.printStackTrace();
}
Or for "SA-25-MAY":
try {
String str_date = "SA-25-MAY";
// remove SA
str_date = str_date.replaceFirst("..-", "");
// add current year
Calendar c = Calendar.getInstance();
str_date = c.get(Calendar.YEAR) + "-" + str_date;
// parse date
Date date;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MMM");
date = formatter.parse(str_date);
System.out.println("Today is " + date);
// add day
c.setTime(date);
c.add(Calendar.DATE, 1);
// rebuild the old pattern with the new date
SimpleDateFormat formatter2 = new SimpleDateFormat("EEE-dd-MMM");
String tomorrow = formatter2.format(c.getTime());
tomorrow = tomorrow.toUpperCase();
tomorrow = tomorrow.substring(0, 2) + tomorrow.substring(3);
System.out.println("Tomorrow is " + tomorrow);
} catch (Exception e) {
e.printStackTrace();
}

Categories

Resources