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);
Related
I have Calendar value which is from Date Picker and I want to convert this value to String format "yyyy-MM-dd"
This is my Date Picker code. I save the selected values to startYear, startMonth and startDay.
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear+ "-" + (startMonth+1) + "-" + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE));
dpd.show();
}
});
and then I tried to convert these values to String in this way. But the log result is 0002-12-31 not what I selected from Date Picker.
Calendar start = Calendar.getInstance();
start.set(startYear, startMonth, startDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = start.getTime(); // Convert to Date
String strStartDate = sdf.format(startDate); // Convert to String
Log.d("start", strStartDate); // this result is 0002-12-31
Never use Calendar. That terrible class was supplanted years ago by the modern java.time classes.
LocalDate
Instead, use LocalDate for a date-only value with no time-of-day and no offset or time zone.
LocalDate x = LocalDate.now() ;
String output = x.toString() ;
Build from parts.
LocalDate x = LocalDate.of( y , m , d ) ;
Get parts.
int y = x.getYear() ;
int m = x.getMonthValue() ;
int d = x.getDayOfMonth() ;
If your code goes like this; the log shows the date value before picking any dates. So you see the value for year: 0, month: 0 and day:0 (which is shown as 0002-12-31). (Note that the code inside onDateSet() method runs asynchronously, when the user picks a date.)
int startYear, startMonth, startDay;
Calendar today = GregorianCalendar.getInstance();
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear+ "-" + (startMonth+1) + "-" + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE));
dpd.show();
}
});
// startYear, startMonth, startDay are not initialized yet and equal 0.
Calendar start = Calendar.getInstance();
start.set(startYear, startMonth, startDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = start.getTime(); // Convert to Date
String strStartDate = sdf.format(startDate); // Convert to String
Log.d("start", strStartDate); // this result is 0002-12-31
Instead; if you try to convert your Date to String "after the values are set" (Shown below) .You can see it will be correctly converted.
int startYear, startMonth, startDay;
Calendar today = GregorianCalendar.getInstance();
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear+ "-" + (startMonth+1) + "-" + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
//************************************************************
//Now we have the picked values.
Calendar start = Calendar.getInstance();
start.set(startYear, startMonth, startDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = start.getTime(); // Convert to Date
String strStartDate = sdf.format(startDate); // Convert to String
Log.d("start", strStartDate); // this result is 0002-12-31
//************************************************************
}
}, today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE));
dpd.show();
}
});
Let's see the simple code to convert Date to String
Basic
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String strDate = dateFormat.format(date);
System.out.println("Converted String: " + strDate);
Advanced
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String strDate = formatter.format(date);
System.out.println("Date Format with MM/dd/yyyy : "+strDate);
formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
strDate = formatter.format(date);
System.out.println("Date Format with dd-M-yyyy hh:mm:ss : "+strDate);
formatter = new SimpleDateFormat("dd MMMM yyyy");
strDate = formatter.format(date);
System.out.println("Date Format with dd MMMM yyyy : "+strDate);
formatter = new SimpleDateFormat("dd MMMM yyyy zzzz");
strDate = formatter.format(date);
System.out.println("Date Format with dd MMMM yyyy zzzz : "+strDate);
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
strDate = formatter.format(date);
System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z : "+strDate);
}
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);
}
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();
}
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 "";
}
}
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();
}