Different result of "yyyy/mm/dd" and "yyyy/mm/DD" - java

I know that to receive date I have to use this code with dd in lowercase:
DateFormat dateFormat0 = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date dateNow0 = new Date();
System.out.println("Date format: " + dateFormat0.format(dateNow0));
and the output is:
Date format: 2016/28/06 09:03:493
But when I use DD in uppercase in date format. code:
DateFormat dateFormat1 = new SimpleDateFormat("yyyy/mm/DD HH:MM:SS");
Date dateNow1 = new Date();
System.out.println("Date format: " + dateFormat1.format(dateNow1));
The output is:
Date format: 2016/28/66 09:03:494
Why I received different result using capital 'DD' instead of 'dd'?
What the result of 'DD' stands for?

D is Day in year (1-365)
d is day in month (1-31)
See the docs on SimpleDateFormat: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

The proper solution code in Java 7 is:
DateFormat dateFormat3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat3.format(cal.getTime()));
and in Java 8:
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String text = localDateTime.format(formatter);
System.out.println(text);

Related

How to remove year in DateFormat follower locale?

I use DateFormat to show Day and Month. DateFormat supports MEDIUM, LONG, FULL all have a year. I want to remove year from this code and how can I achieve this
SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, locale);
sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
But I get an error with some locale like:
bo_CN : སྤྱི་ལོ་y MMMMའི་ཙེས་dད
vi: 'Ngày' dd 'tháng' MM 'năm' y
se_SE: d 'de' MMMM 'de' y
You can use SimpleDateFormat:
Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
String dateString = df.format(date);
Output:
01/21
Try with the below code
String shortDate = "dd.MM";
String mediumDate = "MMM dd";
String longDate = "MMMM dd";
String fullDate = "EEEE, MMMM dd";
Locale locale = new Locale("EN");
DateFormat df = new SimpleDateFormat(fullDate, locale); \\Use the required format here
String formattedDate = df.format(new Date());
I have removed the year part and created custom equivalent of
DateFormat.SHORT as shortDate
DateFormat.MEDIUM as mediumDate
DateFormat.LONG as longDate
DateFormat.FULL as fullDate

Unable to get only the day of date from the date format?

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);

How to parse values returned by getDate() in java?

I have created a web service which returns the date of an event which is initially captured by the getDate() function. I want the date returned by this function (something along this format : 2013-05-17 14:52:00.943) to be parsed and shown to the user in the DD-MM-YYYY format.
Any suggestions? I haven't found any solution along this direction yet.
I have tried this code and it's work fine for me,Please Try my code below: Please upvote to Tarun also coz he gave almost right answer.just he did mistake that he passes cal.getTime() method instead of pDate
String formatDate = p.format(pDate);
and second mistake in format like"DD-MM-YYYY" but actual format is:
"dd-MM-yyyy" not "DD-MM-YYYY"
I have done changes in it and modify it.
String dateStr = "2013-05-16 14:52:00.943";
SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S"); // your web service format
Date pDate = c.parse(dateStr);
SimpleDateFormat p = new SimpleDateFormat("dd-MM-yyyy"); // your required format
String formatDate = p.format(pDate); // convert it in your required format
SimpleDateFormat formatter = new SimpleDateFormat("EEEE"); // Day format as you want EEE for like "Sat" and EEEE for like "Saturday"
String Day = formatter.format(pDate); // This will give you a day as your selected format
System.out.println("Date & Day>>>"+formatDate+" "+Day);
// For GMT format your format should be like this: "2013-05-16 14:52:00.943 GMT+05:30"
// Give it to me in GMT time.
c.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
System.out.println("GMT time: " + c.format(pDate));
Output:
Date & Day>>>16-05-2013 Thursday
GMT time: 2013-05-16 02:52:00.943 Greenwich Mean Time
Joda time:
you can download 2.0 jar file of joda time from here:
DateTimeFormatter jodaFormatter = ISODateTimeFormat.dateTime();
DateTime jodaParsed = jodaFormatter
.parseDateTime("2013-05-17T16:27:34.9+05:30");
Date date2 = jodaParsed.toDate();
System.out.println("Date & Day:" + jodaParsed.getDayOfMonth() + "-" + jodaParsed.getMonthOfYear() + "-" + jodaParsed.getYear() + " " + jodaParsed.getHourOfDay() + ":" + jodaParsed.getMinuteOfHour()+" "+jodaParsed.dayOfWeek().getAsText());
output:
Date & Day:17-5-2013 16:27 Friday
Hope it will work for you.
String dateStr = "2013-05-17 14:52:00.943";
SimpleDateFormat c = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
Date pDate = c.parse(dateStr);
SimpleDateFormat p = new SimpleDateFormat("dd-MM-yyyy");
String formatDate = p.format(pDate);
You can use Joda Time if you have colon in time offset.
DateTimeFormatter jodaFormatter = ISODateTimeFormat.dateTime();
DateTime jodaParsed = jodaFormatter.parseDateTime("2013-05-17T16:27:34.9+05:30");
Date date = jodaParsed.toDate();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.get(Calendar.DATE));
More info about joda can be found here.
Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.
Example:
SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("05/18/2013");
System.out.println(format2.format(date));
Output:
11-05-2013
Edit:
Calendar cal = Calendar.getInstance();
cal.setTime(specific_date);
int dayOfMonth = cal.get(Calendar.DAY_OF_WEEK);
String dayOfMonthStr = String.valueOf(dayOfMonth);

How to convert date and time stored in string to 24 format time in android?

I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format

change value of a timestamp format into other timestamp format?

Hi can anyone tell me how can i change a given timestamp format into other timestamp format
like
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar d1 = Calendar.getInstance();
d1.setTime(formatter.parse("2012-03-08 17:32:56"));
to
"dd-MMM-yyyy HH:mm:ss"
format
format the date with new date format dd-MMM-yyyy HH:mm:ss
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldFormatedDate = formatter.parse("2012-03-08 17:32:56");
System.out.println(new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss").
format(oldFormatedDate));
Continue with DateFormat#format(Date):
SimpleDateFormatter newFormat = new SimpleDateFormatter("dd-MMM-yyyy HH:mm:ss");
String newFormattedDate = newFormat.format(d1);

Categories