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 5 years ago.
Improve this question
I want date without time in java
unix format is
YYYYMMDDHHMMSS.miliseconds
unix string 20170817134131.384
string val = "20170817134131.384";
if (val != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
try {
date = df.parse(String.valueOf(val));
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date: ", e);
}
return date;
}
Here's a simple solution to convert unix time to localDate and then you're free to format it as you please:
long time = 1491231860;
final LocalDate localDate = LocalDate.fromMillisSinceEpoch(unixSeconds);
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am was writing some code, but I´m not sure which is better. In one way it's easier read what is happening, but I have more line of code.
In the other way, you has less line of code but I think is harder to understand.
String imp = importance.getSelectedItem().toString();
String title_str = title.getText().toString();
String body_str = body.getText().toString();
String location_str = location.getText().toString();
int day = date.getDayOfMonth();
int month = date.getMonth()+1;
int year = date.getYear();
int hh = time.getCurrentHour();
int mm = time.getCurrentMinute();
String date_str = year+"/"+month+"/"+day+" " + hh+":"+mm +":00"; // yyyy/MM/dd HH:mm:ss
long dateMilliseconds = new Timeconversion().timeConversion(date_str);
Conference conference = ConferenceBuilder.conference()
.id(idConf)
.importance(Double.parseDouble(imp))
.title(title_str)
.body(body_str)
.location(location_str)
.timeInMilliseconds(dateMilliseconds)
.build();
or
Conference conference2 = ConferenceBuilder.conference()
.id(idConf)
.importance(Double.parseDouble(importance.getSelectedItem().toString()))
.title(title.getText().toString())
.body(body.getText().toString())
.location(location.getText().toString())
// yyyy/MM/dd HH:mm:ss
.timeInMilliseconds(new Timeconversion().timeConversion(date.getYear()+"/"+date.getMonth()+1+"/"+date.getDayOfMonth()+" " + time.getCurrentHour()+":"+time.getCurrentMinute() +":00"))
.build();
Split the difference. I'd do something like this:
Conference conference2 = ConferenceBuilder.conference()
.id(idConf)
.importance(Double.parseDouble(importance.getSelectedItem().toString()))
.title(title.getText().toString())
.body(body.getText().toString())
.location(location.getText().toString())
// yyyy/MM/dd HH:mm:ss
.timeInMilliseconds(getTimeInMillis(datePicker, timePicker))
.build();
}
private long getTimeInMillis(DatePicker datePicker, TimePicker timePicker) {
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
return calendar.getTimeInMillis();
}
I don't think that extracting String objects from your textviews makes things any more readable, since your textviews are pretty clearly named.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to parse this String to timestamp timezone issue ?
String timestamp = "29-JAN-2014 01:00:00.000 PM EUROPE/PARIS";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS aa ZZZ");
Date dateFormatted = null;
try {
dateFormatted = df.parse(timestamp);
} catch (ParseException execption) {
execption.printStackTrace();
}
This seems to work:
TimeZone tz = TimeZone.getTimeZone("Europe/Paris")
String timestamp = "29-JAN-2014 01:00:00.000 PM";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS aa");
df.setTimeZone(tz)
try {
dateFormatted = df.parse(timestamp);
} catch (ParseException execption) {
execption.printStackTrace();
}
But you have to split the timezone from the timestamp
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a date in string format like this 2/14/2014. I want to convert this date to oracle.jbo.Date Object in java. How can i do that?
-Thanks
Dont give the month as 2/14/2014. it should be 02/14/2014.
if(inputDate!=null){
try {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
date = formatter.parse(inputDate);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(sqlDate);
System.out.println(jboDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
You can try the same with SimpleDateFormat also.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Please help me convert time from 12 hours to 24 hours format return time should be in UTC in JAVA
This will help you to convert time to 24 Hour
public static String convertTo24Hour(String Time) {
DateFormat f1 = new SimpleDateFormat("hh:mm a"); //11:00 pm
Date d = null;
try {
d = f1.parse(Time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateFormat f2 = new SimpleDateFormat("HH:mm");
String x = f2.format(d); // "23:00"
return x;
}
Later on you may convert to UTC as per your wish or you may compile code in one function
public static String CalculateUTC_Time(String Date, String Time) {
String X[] = Time.split(":");
int Hours = Integer.parseInt(X[0]);
int Minutes = Integer.parseInt(X[1]);
LocalDate date = new LocalDate(Date);
LocalTime time = new LocalTime(Hours, Minutes);
DateTime dt = date.toDateTime(time);
SimpleDateFormat f2 = new SimpleDateFormat("HH:mm");
f2.setTimeZone(TimeZone.getTimeZone("UTC"));
return (f2.format(dt.toDate()));
}
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 9 years ago.
Improve this question
I have a String stating the date as "2012-12-31" how can I convert it into date format using java and store it MySQL database where the type in MySQL is of date type.
You can do as follows
String string = "2012-12-31";
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date date = formatter.parse(string);
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); // convert java.util.date to java.sql.date
Have a look at SimpleDateFormat
String string = "2012-12-31"; //You have like this now
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
Date date = formatter.parse(string);
System.out.println(date);