This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 6 years ago.
I want to show the current date in my application like this:
Thu, May 2, 2013
I already have the following code to get the current date
Calendar c = Calendar.getInstance();
Time time = new Time();
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH),
c.get(Calendar.YEAR));
How can I format this Time object to the string I need?
This does what you want
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);
Where strDate can be displayed in your textView or whatever
Maybe you can use it.
This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.
import java.text.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Date dt = new Date(1000000000000L);
DateFormat[] dtformat = new DateFormat[6];
dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
for(DateFormat dateform : dtformat)
System.out.println(dateform.format(dt));
}
}
output:
9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01
Source
Use this
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd,yyyy");
String formattedDate = formatter.format(new Date(System.currentTimeMillis()));
Log.e("formattedDate",formattedDate);
I will suggest to use java.text.SimpleDateFormat instead.
public static void main(String[] args) {
Date date=new Date();
String format = new SimpleDateFormat("EEE,MMM d,yyyy ").format(date);
System.out.println(format);
}
SimpleDateFormat dateformat= new SimpleDateFormat("dd,MM,yyyy");
String strdate = dateformat.format(new Date(System.currentTimeMillis()));
Oops, I'm a bit slow.
Related
I have this string: 2018-09-22 10:17:24.772000
I want to convert it to Date:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
but it shows: Sat Sep 22 10:17:24 GMT+03:30 2018
Here is what you should do instead, you are printing date object itself, you should print its format.
I will provide the code with old date api and new local date api :
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
String sdate = "2018-09-22 10:17:24.772000";
Date dateFrom = simpleDateFormat.parse(sdate);
System.out.println(dateFrom); // this is what you do
System.out.println(simpleDateFormat.format(dateFrom)); // this is what you should do
// below is from new java.time package
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
System.out.println(LocalDateTime.parse(sdate, formatter).format(formatter));
output is :
Sat Sep 22 10:30:16 EET 2018
2018-09-22 10:30:16.000000
2018-09-22 10:17:24.772000
Hope This will help you
public class Utils {
public static void main(String[] args) {
String mytime="2018-09-22 10:17:24.772000";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSS");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
String finalDate = timeFormat.format(myDate);
System.out.println(finalDate);
}
}
Looks to me like you have converted it to a Date. What is your desired result? I suspect what you are wanting to do is to create another Simple date format that shows your expected format and then use simpledateformat2.format(dateFrom)
I should also point out based on past experience that you should add a Locale to your simple date formats otherwise a device with a different language setting may not be able to execute this code
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.US);
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 7 years ago.
I see that this is deprecated in Java:
Date origin = new Date("July 5, 2318 12:00:00");
What I'm trying to figure out is what I can do to get that exact same functionality. The full context of my example is this:
Date origin = new Date("July 5, 2318 12:00:00");
double stardatesPerYear = 56844.9 * 34367056.4;
double milliseconds = origin.getTime() + stardatesPerYear;
Date dateResult = new Date();
dateResult.setTime((long)milliseconds);
The result of that is that dateResult will a Date object with the following string representation:
Sat May 31 12:24:44 CDT 2380
I'm not concerned about the exact format of the date as I am the accuracy of the result as well as the fact that it can be parsed.
I've seen references to using Calendar and SimpleDateFormat but none of those seem to get me the same output, likely because I'm doing something wrong.
UPDATE ON POSSIBLE DUPLICATE:
I don't know if my question is a duplicate as suggested, but it may be. I tried some examples like this:
String string = "July 5, 2318";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
try {
Date date = format.parse(string);
} catch (ParseException e) {
e.printStackTrace();
}
That seems like a lot to replace my original Date origin statement.
I guess what I'm curious about is given my full code example, which is this:
Date origin = new Date("July 5, 2318 12:00:00");
double stardatesPerYear = 56844.9 * 34367056.4;
double milliseconds = origin.getTime() + stardatesPerYear;
Date dateResult = new Date();
dateResult.setTime((long)milliseconds);
What's the most effective and efficient code to replace the first line of that code, since it is indicated to me that my first line is using deprecated functionality?
This works, tested in Java 8:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMMM d, yyyy HH:mm:ss");
Date origin = Date.from(LocalDateTime.parse("July 5, 2318 12:00:00", dtf).toInstant(ZoneOffset.UTC));
Using SimpleDateFormat one line is changed to two lines:
DateFormat dateformat = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss", Locale.US);
Date date = dateformat.parse("July 5, 2318 12:00:00");
To get same output:
DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss", Locale.US);
Date date = fmt.parse("July 5, 2318 12:00:00");
date.setTime(date.getTime() + (long)stardatesPerYear);
3 lines vs. 4 lines (I don't count stardatesPerYear initialization) :)
You can use any of this way:
1)
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd yyyy hh:mm:ss");
String dateInString = "July 5, 2318 12:00:00";
Date date = sdf.parse(dateInString);
2)
Calendar calendar = new GregorianCalendar(2318,Calendar.JULY,5, 12, 0, 0);
Date date = calendar.getTime();
3)
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR, 2318);
calendar.set(Calendar.MONTH, Calendar.JULY);
calendar.set(Calendar.DAY_OF_MONTH, 5);
calendar.set(Calendar.HOUR, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 8 years ago.
Server sending me time as 1390361405210+0530 so if I want to convert this in to date then should I have to add 0530 into 1390361405210 and then calculate date and time?
Any suggestion should be appreciated.Thanks
How about this.
long currentDateTime = 1390361405210L;
Date currentDate = new Date(currentDateTime);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss Z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+530"));
System.out.println(sdf.format(currentDate));
public static void main( String[] args )
{
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
long milliSeconds=1390361405210L;
Date date = new Date(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
System.out.println(formatter.format(calendar.getTime()));
System.out.println(formatter.format(date));
}
If we consider that the first part of the String is the number of milliseconds since the epoch, and the second part is a timezone indication (in that case, IST, Indian Standard Time), you can get a readable date like this :
final String jsonDate = "1390361405210+0530";
final Date date = new Date(Long.parseLong(jsonDate.substring(0, jsonDate.length() - 5)));
final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT" + jsonDate.substring(jsonDate.length() - 5)));
System.out.println(format.format(date));
Output:
January 22, 2014 9:00:05 AM GMT+05:30
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
9/4/2014 3:55:10 AM
here is my current date and time i want to add +10 hour so that i cam match with Current time of device please tell me how we can implement
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String currentDateandTime =" 9/4/2014 3:55:10 AM ";
Date date = formatter.parse(currentDateandTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here "+calendar.getTime());
}
This Code am trying but i am not able to Impalement please help me where am doing mistake .
Your code is almost working, but you have made a typo. You try to invoke parse() on formatter which has not been declared. Instead you have to call parse() on sdf:
public static void main(final String[] args) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
final String currentDateandTime = " 9/4/2014 3:55:10 AM ";
final Date date = sdf.parse(currentDateandTime);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here " + calendar.getTime());
}
Since you are using 12-hour system you can modify it like this:
public static void main(final String[] args) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
final String currentDateandTime = "9/4/2014 3:55:10 AM";
final Date date = sdf.parse(currentDateandTime);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here " + sdf.format(calendar.getTime()));
}
If you operate on dates after 1970, adding 10 hours to given date can be achieved in one line of code:
Date d1 = new Date(); // or sdf.parse()
Date d2 = new Date( d1.getTime() + 10 * 60 * 60 * 1000 ); // add 10h in millis
Output is:
Thu Sep 04 13:56:39 CEST 2014
Thu Sep 04 23:56:39 CEST 2014
First if you want to figure out the current time of your computers clock you can do the following:
long now = System.currentTimeMillis();
Which will return a long representing that timestamp. If you want it as a Date object you can do:
Date now = new Date();
As for the rest of your code the logic looks correct here is a snippet of my code that adds 10 hours to now.
Date now = new Date();
Calendar cal = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
Date plus10 = calendar.getTime();
System.out.println(plus10);
You didn't create a DateFormat. Try this:
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String currentDateandTime = " 9/4/2014 3:55:10 AM ";
DateFormat formatter = DateFormat.getInstance();
Date date = formatter.parse(currentDateandTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here " + calendar.getTime());
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my code I am getting date as this String:
date="2013-06-15"
I want it to convert to 15 Jun 2013 . I have tried dateFormat:
DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
String date = df.format(det);
DateFormat f2 = new SimpleDateFormat("dd-mmmm-yyyy");
date=f2.format(date).toLowerCase();
out.println("DATE"+date);
But it gave null pointer Could Anyone help me doing this.Please help?
String dateString="2013-06-15";
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
DateFormat df2 = new SimpleDateFormat("dd MMM yyyy");
System.out.println(df.format(date).toLowerCase());//print 15-jun-2013
System.out.println(df.format(date));//print 15-Jun-2013
System.out.println(df2.format(date));//print 15 Jun 2013
first convert your Date string to Date then convert it to your required format
package naveed.workingfiles;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateToString {
public static void main(String[] args) {
String laDate="2013-06-15";
String dateString = laDate.substring(8, 10) + "/"
+ laDate.substring(5, 7) + "/"
+ laDate.substring(0, 4);
Date date= new SimpleDateFormat("dd/MM/yyyy").parse(dateString);
String dateFormat = "dd-MMM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
String tDate = sdf.format(date);
System.out.println(tDate);//here your String date
}
}
// Create an instance of SimpleDateFormat used for formatting
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String reportDate = df.format(today);
// Print what date is today!
System.out.println("Report Date: " + reportDate);
This will work
String dateStr = "2013-06-15";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date=df.parse(dateStr);
DateFormat f2 = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(f2.format(date));
This is not gonna work: format() does not take String parameters:
String date = df.format(det);
DateFormat f2 = new SimpleDateFormat("dd-mmmm-yyyy");
date=f2.format(date).toLowerCase();
Try this:
String dateAsString = "2013-06-15"
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(dateAsString);
df = new SimpleDateFormat("dd MMM yyyy");
out.println("DATE"+df.format(date));