calender show results like date1<= results<date2 - java

I used two calender for starting date and finishing as below,
date.Ic.add(Restrictions.between("islemZamani", date1, date2));
However result of this criteria has results of date1 and between of them,not include results of date2.I mean ,It shows date1<= results. I want date1<= results<=date2. So I tried SimpleDateFormat like this;
String tar1 = new String();
String tar2 = new String();
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
dateFormat.setTimeZone(tz);
tar1 = dateFormat.format(date1);
tar2 = dateFormat.format(date2);
tar1 = tar1.substring(11, 18) + "000000";
tar2 = tar2.substring(11, 18) + "235959";
c.add(Restrictions.between("islemZamani", tar1, tar2));
Now It gives NullPointerException.How can i solve this problem? Do you suggest any different way from SimpleDateFormat? Thanka for any reply.

Use a string to manipulate date is too complicated and may case a loooot of problems.
Use only java.util.Date and/or java.util.GregorianCalendar.
Try this :
GregorianCalendar calendar1 = new GregorianCalendar();
calendar1.setTime(date1);
// Edit the calendar1 here if you want to
GregorianCalendar calendar2 = new GregorianCalendar();
calendar2.setTime(date2);
calendar2.add(GregorianCalendar.DAY_OF_YEAR, 1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
TimeZone tz = TimeZone.getTimeZone("Europe/Istanbul");
dateFormat.setTimeZone(tz);
String tar1 = dateFormat.format(calendar1.getTime());
String tar2 = dateFormat.format(calendar2.getTime());
c.add(Restrictions.between("islemZamani", tar1, tar2));
But without the trace, I'm not sure this will solve your problem...
And, if it's possible, use the date instead of the string in your restriction.

Related

How to delete some character from respone in Java?

I have some string respone date like this:
2018-11-30 12:00:00
and i want just get the time, so i want like this:
12.00
and my question, can we delete some character like that?
Sounds like you want to use SimpleDateFormat so something like:
Date today = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String folderName = formatter.format(today);
Based on your comment try:
int index = time.indexOf(‘:’);
int start = index -2;
int end = index + 2;
String newTime = time.substring(start,end);
String apiDateString = "2018-11-30 12:00:00"; // in this case, your string from api
Date apiDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(apiDateString); // convert api date string to date
SimpleDateFormat formatter = new SimpleDateFormat("hh.mm");
String yourDateString = formatter.format(apiDate);
I think you want this.
Edited with little comment.
convert string to date. (apiDateString to apiDate)
create new format for what you want to form. In this case, 12.00
convert apiDate to your format. (apiDate -> 12.00)
You can try this
Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdformat = new SimpleDateFormat("HH:mm:ss");
String strDate = "Current Time : " + mdformat.format(calendar.getTime());
display(strDate);
You can use SimpleDateFormat to get time and then replace : by .
I hope it can help your problem!
You can refer to this post.
Use SimpleDateFormat to format you date.
From String to Date
// Convert from string to date
String yourDate = "2018-11-30 12:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(yourDate);
//Use calender instead of date to getHours and minute, due to deprecated
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.get(Calendar.HOUR);
calendar.get(Calendar.MINUTE);
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)+"."+calendar.get(Calendar.MINUTE)); //12.0

How to create specific Date Output as String?

I have this class:
public class Period {
private Date startDate = new Date();
private String startTime = "10:00";
private Date endDate = new Date();
private String endTime = "23:59";
}
Now i need two string outputs "2014-01-08T10:00:00" and "2014-01-08T23:59:00" !
How i can create these two outputs by using the class Period? It is necessary to work with the class Period, which includes startTime and EndTime as Strings!
I only have this small code for using the class SimpleDateFormat:
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Thanks for helping me !
What #tobias_k is saying is this. This is your format
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'");
When you call .format to format the Date, it returns a String
String dateString = format.format(period.getStartDate());
Then concatenate it
dateString = dateString + period.getStartTime();
Try this: Using GregorianCalendar
Date startDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
GregorianCalendar cal = new GregorianCalendar();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 10, 00, 00);
System.out.println(dateFormat.format(cal.getTime()));
Update1
To get the string value in variable:
String str = dateFormat.format(cal.getTime());
System.out.println(str);

Calculating Date in Java

Ok so I am trying to create a date in this format:
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
I am having trouble calculating that date so that it gives me 1/1/13.
Date newdate = new Date (136199001);
String date = dateformat.format(newdate);
However I can't work out how to do it to get to my desired date. I know I am suppose to work it out from 01/01/70 but I am having trouble. The question : what is the formula to work the date out?
I would say that what you are looking for is this:
new SimpleDateFormat("dd/MM/yy").parse("1/1/13");
You can use calendar object for a specific date. It is much easier.
Calendar cal = Calendar.getInstance();
cal.set(2013, 0, 1); //1st january 2013
Date date = cal.getTime();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yy");
String dateStr = dateformat.format(date);

how to Convert PST to GMT(-0800) in java?

Actually in my java program like the following code...
String date1=null;
String formate="IST";
SimpleDateFormat sourceFormat = new SimpleDateFormat("z");
SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('Z')'");
date1 = gmtFormat.format(sourceFormat.parse(formate));
System.out.println(date1);//output GMT(+0530)
Hear it is giving Correct Value but the timezone may change like PST---- GMT(-0800) will so.
But my code alway show only GMT(+0530)
Please help me to convert timezone ACT,,PST,IST.....etc to GMT(+11:00),GMT(-08:00),GMT(+0530).......etc
java.text.SimpleDateFormat sourceFormat = new SimpleDateFormat("z");
java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('ZZZ')' zzzz");
java.util.Date date1 = sourceFormat.parse("IST");
TimeZone gmtTime = TimeZone.getTimeZone("IST");
gmtFormat.setTimeZone(gmtTime);
//System.out.println("Source date: " + date1);
System.out.println(" "+ gmtFormat.format(date1));
Way cleaner in my opinion.
TimeZone gmtTime = TimeZone.getTimeZone("IST");
long gmtOffset = gmtTime.getOffset(new Date().getTime())/ TimeUnit.HOURS.toMillis(1);

Convert date to XMLGregorianCalendar

I have a date in the following format in UI..
Eg: Thu. 03/01
I convert them to XMLGregorianCalendar as below explained.
final DateFormat format = new SimpleDateFormat("E. M/d");
final String dateStr = closeDate;
final Date dDate = format.parse(dateStr);
GregorianCalendar gregory = new GregorianCalendar();
gregory.setTime(dDate);
XMLGregorianCalendar dealCloseDate = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(gregory);
My Output is "3/06/70 05:00 AM" instead of "3/06/2011 05:00 AM". What is the chnage required to get the proper year.
You didn't mention anything about how the year is supposed to be represented in this date conversion, but here is some pseudocode to get you started. Note that I don't explicitly deal with the timezone in this example:
final DateFormat format = new SimpleDateFormat("E. M/d");
final String dateStr = "Thu. 03/01";
final Date date = format.parse(dateStr);
GregorianCalendar gregory = new GregorianCalendar();
gregory.setTime(date);
XMLGregorianCalendar calendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(
gregory);
I did the following code to achieve the same. May be a lengthy code but is working fine.
def gregorianCalendar = new GregorianCalendar()
def xmlGregorianCalendar = newInstance().newXMLGregorianCalendar(gregorianCalendar)
if (//past date is needed) {
def date = xmlGregorianCalendar.toGregorianCalendar().time
def cal = Calendar.getInstance()
cal.setTime(date)
cal.add(Calendar.DATE,-3); //subtract 3 days
date.setTime(cal.getTime().getTime())
gregorianCalendar.setTime(date)
xmlGregorianCalendar = newInstance().newXMLGregorianCalendar(gregorianCalendar)
}

Categories

Resources