Java Formatting Date - java

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.

Related

date_time difference output is not correct

I have written this java code to get the difference between system_date_time and my_date_time, when I run this code system_date_time was 2015-02-19 06:01:00, so the difference should be 24 hours = 86400 seconds, but this code is giving -2592002 as output.
So please help to fix the problem.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class A {
public static void main(String arg[]) throws ParseException {
String dateString = "2015-02-20 06:01:00"; // end_date_time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date myDate = sdf.parse(dateString);
System.out.println( (myDate.getTime() - System.currentTimeMillis()) / 1000 );
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Note the MM (MM is Month, mm is minute)
You parsed the Date wrong (so it gave you sometimes in January)
You can simply use java.sql.Timestamp for what you trying to achieve.
String dateString = "2015-02-20 06:01:00"; // end_date_time
Timestamp myTimeStamp = Timestamp.valueOf(dateString);
System.out.println( ((myTimeStamp.getTime() - System.currentTimeMillis()) / 1000));
Change your date format to
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
MM is for months mm is for minutes
Update code here, first change is use MM instead of mm in SimpleDateFormat and another change in sysout
String dateString = "2015-02-18 15:39:00"; // end_date_time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date myDate = sdf.parse(dateString);
System.out.println( ( System.currentTimeMillis()- myDate.getTime()) / 1000 );

Current Date in Java 1.4

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

how to get date and time in java like this 02-Oct-12 12:58:20 AM

i have tried some thing like this
package com.poc;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
}
and my out put is : 2013-01-07 17:12:27 but want the output to be like this 2013-Jan-07 17:12:27 PM
how to do this in java?
Best Regards
your format should be
new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
07-Jan-13 07:20:02 PM
new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
07-Jan-13 19:20:02
Your format should be
yyyy-MMM-dd hh:mm:ss a
use this instead:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss a");
See Class SimpleDateFormat
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
So MM is iterpreted as number and MMM or more is interpreted as text (i.e. month name)

Issue in showing 24-Hour Format time

I have used following line code to display time in 24-Hour format from the Calendar Instance
Everything show correctly, but only problem is while showing time at midnight 12:00 am, It show time as 24:00 instead of showing 00:00. Why this happen anything wrong in my code.
Calendar m_CalInstance = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("kk:mm");
String timeDisplay = formatter.format(m_CalInstance.getTime());
SimpleDateFormat formatter = new SimpleDateFormat("kk:mm");
should be
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
Check this javadoc for more information on SimpleDateFormat
use capital letters KK inplace of kk here
SimpleDateFormat formatter = new SimpleDateFormat("KK:mm");

SimpleDateFormat returns 24-hour date: how to get 12-hour date?

I want current time in millis and then to store it in 12 hour format but with this piece of code I am getting 24 hour format time.
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss a");
dateforrow = dateFormat.format(cal1.getTime());
can anybody suggest modifications to get the desired results?
Change HH to hh as
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy hh:mm:ss a");
dateforrow = dateFormat.format(cal1.getTime());
Note that dd/mm/yyyy - will give you minutes instead of the month.
Referring to SimpleDataFormat JavaDoc:
Letter | Date or Time Component | Presentation | Examples
---------------------------------------------------------
H | Hour in day (0-23) | Number | 0
h | Hour in am/pm (1-12) | Number | 12
I re-encounter this in the hard way as well. H vs h, for 24-hour vs 12 hour !
Yep, confirmed that simply using "hh" instead of "HH" fixed my issue, Since "hh" is for 12-Hour Format & "HH" is for 24-Hour Format.
Changed from this:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm aa");
To this:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
You can still use "HH" to store the time if you don't want to bother storing and dealing with the AM/PM. Then when you retrieve it, use "hh".
Hi I tested below code that worked fine :
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
dateFormat.format(cal1.getTime());
There are three major problems with your code:
Using m [Minute in hour] at the place of M [Month in year].
Using H [Hour in day (0-23)] instead of h [Hour in am/pm (1-12)]. Check the documentation to learn more about these two points.
Not using Locale with SimpleDateFormat. Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it.
So, the instantiation with the correct format would be:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a", Locale.ENGLISH);
java.time
Note that the java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*, released in March 2014 as part of Java SE 8 standard library.
Solution using java.time, the modern Date-Time API:
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.now(ZoneOffset.UTC);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/uuuu hh:mm:ss a", Locale.ENGLISH);
String formatted = dtf.format(odt);
System.out.println(formatted);
}
}
Here, you can use y instead of u but I prefer u to y.
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
You can try it like this
Calendar c= Calendar.getInstance();
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
String str=sdf.format(c.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
use hh in place of HH
Simply follow the code
public static String getFormatedDate(String strDate,StringsourceFormate,
String destinyFormate) {
SimpleDateFormat df;
df = new SimpleDateFormat(sourceFormate);
Date date = null;
try {
date = df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat(destinyFormate);
return df.format(date);
}
and pass the value into the function like that,
getFormatedDate("21:30:00", "HH:mm", "hh:mm aa");
or checkout this documentation SimpleDateFormat for StringsourceFormate and destinyFormate.
See code example below:
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
String formattedDate = df.format(new Date());
out.println(formattedDate);

Categories