For getting Current date in mm/dd/yyyy format I am using the below code
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
Date date = new Date();
String date3= sdf.format(date);
date = sdf.parse(date3);
but everytime I print the date ,it gives me wrong and random output
output
Currentd Date:: 49/22/2013
output
Currentd Date:: 07/22/2013
Kindly suggest as what I should use to get current date.
The Java Version I am using is 1.4
Change "mm/dd/yyyy" into "MM/dd/yyyy". m(lowercase) is use for minutes not for month. For month you should use M(uppercase)
You might want to use MM instead of mm in the format pattern which will give you month instead of minutes.
Use MM/dd/yyyy
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
MM - Month
mm - Minute
m = Minute
M = Month
Thus you have to use "MM/dd/yyyy"
Try
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println(sdf.format(date));
Try
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22OR
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime()); //2014/08/06 16:00:22
Related
static DateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy HH:mma");
static DateTimeZone zone = DateTimeZone.forID("America/New_York");
static Chronology coptic = GJChronology.getInstance(zone);
static DateTime dt = new DateTime(coptic);
System.out.println("time is :::"+ dateFormat.format(dt.toDate()));
I am using the above piece of code to get a system time and it installed in websphere server.
Actually, its returning only server start up time not current time.Please guide me to get current time.
If you want the current date and time, you can use one of the following:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String currentDate = dateFormat.format(date));
or using Calendar.getInstance(),
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String currentDate = dateFormat.format(cal.getTime());
Javadoc of GJChronology says:
Wherever possible, it is recommended to use the ISOChronology instead.
So you should just use:
DateTime dt = DateTime.now();
For printing the date/time, you should either lowercase the HH or remove the a, since 24-hour clock with AM/PM is wrong:
DateTimeFormatter dateFormat = DateTimeFormat.forPattern("MMM dd yyyy hh:mm a")
.withZone(DateTimeZone.forID("America/New_York"));
System.out.println("time is :::"+ dt.toString(dateFormat));
I am given an input date string for ex:2015-06-02 12:60:30 and the output should be 2015-06-02 00:00:00 i.e how to set the HH:mm:ss to zero in the given format yyyy-MM-dd HH:mm:ss ?
use yyyy-MM-dd 00:00:00 format instead of yyyy-MM-dd HH:mm:ss
it will change the hours, minutes and seconds to zero instead of actual values
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String dateValue = dateFormat.format(new Date());
System.out.println(dateValue);
You can try to use this:
calendar.set(Calendar.HOUR_OF_DAY, 0);
Something like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
System.out.println(sdf.format(c.getTime()));
c.set(Calendar.HOUR_OF_DAY, 0);
System.out.println(sdf.format(c.getTime()));
Simply provide a format for the portion of the "date" you want to keep, for example...
String text = "2015-06-02 12:60:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(text);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(out.format(date));
Outputs...
2015-06-02 00:00:00
This is a little trick, which is actually described in the JavaDocs
Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.
Emphasis added by me
try this
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
String strDate = sm.format(myDate);
If you don't require validation of the input format, you could use a regular expression:
input.replaceAll("\d\d:\d\d:\d\d", "00:00:00")
However, note that this conversion is not necessarily one which yields a valid time: midnight might not be valid, depending upon the date you are converting and its time zone, so this might not yield a valid time. (The start of daylight savings time in Asia/Gaza is the oft-cited example).
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.MONTH,6);
cal.set(Calendar.HOUR_OF_DAY,2);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
Date d = cal.getTime();
And to format it you can use:
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatted = sdFormat.format(cal.getTime());
How to get the date from the date format
dd/MM/yyyy
Example:
04/05/2015
I only need the date as 04.
Here is code snippet:
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH,1);
System.out.println(dateFormat.format(date));
How to solve this issue?
Use cal.get(Calendar.DAY_OF_MONTH).
You need to change pattern which shows to you desired data format:
DateFormat dateFormat = new SimpleDateFormat("dd");
instead of
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
UPDATE:
Java 1.8 has updated data and time API.
Here is snippet of code which I checked:
LocalDate lastAprilDay = LocalDate.of(2014, Month.APRIL, 30);
System.out.println("last april day: " + lastAprilDay);
LocalDate firstMay = lastAprilDay.plusDays(1);
System.out.println("should be first may day: " + firstMay);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd");
String formatDate = formatter.format(firstMay);
System.out.println("formatted date: " + formatDate);
Output:
last april day: 2014-04-30
should be first may day: 2014-05-01
formatted date: 01
For more info see Java documentations to this classes:
LocalDate
DateTimeFormatter
Simply change DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); to DateFormat dateFormat = new SimpleDateFormat("dd");
or use cal.get(Calendar.DAY_OF_MONTH);
I want to increment the date by one. I have the below code while running the code I am getting unparseable date finally I want the date as string in the format of MM-DD-YYYY.
But same program is working with the YYYY-MM-DD format but i want mydate in this format(MM-DD-YYYY)
String dt = schReq.getStartDate(); // Start date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, days); // number of days to add
dt = sdf.format(c.getTime());
schReq.setStartDate(dt);
Can anyone please help me?
The code should be working fine as long as dt and days are correct. This gave me 12-18-2014:
String dt = "12-17-2014"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime());
You have to use two different DateFormats:
one for parsing the string and one for formatting.
String dt = schReq.getStartDate(); // Start date
SimpleDateFormat sdf_parser = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf_parser.parse(dt));
c.add(Calendar.DATE, days); // number of days to add
dt = sdf.format(c.getTime());
schReq.setStartDate(dt);
I am trying to get the current date and format it however i am getting an invalid month of 59. Under is the code
Code
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
Date todayDate = new Date();
String formatDate = df.format(todayDate);
Output is 2013-59-07
You have used mm which means minutes. Use capital MM instead.
You can find all date and time pattern symbols on the SimpleDateFormat Javadoc page.
You need "MM" for month. "mm" is for minutes.
See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You need to use capital MM and not mm. Lower case 'mm' is for minutes and not for months.
So, your code would be :
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Month is retrieved by MM, not mm.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Refer to SimpleDateFormat JavaDoc:
M Month in year
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String dt = dateFormat.format(cal.getTime());
Use MM instead of mm.