How can I avoid a NullPointerException here if the user passes a null value for a date? Ideally, I would use a simple if statement.
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
String layupDate = dateFormatter.format(orderLayupDateSet.getLayupDate());
String layupTime = timeFormatter.format(orderLayupDateSet.getLayupDate());
if(layupDate == null) {
}
You validate the date (orderLayupDateSet.getLayupDate()) in a liner (just creating a new date if null here but you can do as you please);
(orderLayupDateSet.getLayupDate() == null) ? new Date() : orderLayupDateSet.getLayupDate()
You can also go the traditional
if (orderLayupDateSet.getLayupDate() == null) {
orderLayupDateSet.setLayupDate(new Date());
}
Or you can do it the other way around with a utility, and calling the utility function to check for you - seeing that you return a String in your example above. Like the below;
public class DateUtils {
public static String formatDateTime(Date dateOrNull) {
return (dateOrNull == null ? null : DateFormat.getDateTimeInstance().format(dateOrNull));
}
}
Which in your code can be
String layupDate = (orderLayupDateSet == null || orderLayupDateSet.getLayupDate() ? null : dateFormatter.format(orderLayupDateSet.getLayupDate()));
Related
I am trying to check if 'toDate' lies within the range , for that i tried 2 approaches but both haven't worked for me
approach 1:
if (MyEntity.getFromDate() != null
&& MyEntity.getToDate() != null)) {
predicate.getExpressions()
.add(criteriaBuilder.between((root.<Date>get("toDate")), MyEntity.getFromDate(),MyEntity.getToDate()));
approach 2
if(MyEntity.getFromDate() != null
&& MyEntity.getToDate() != null) {
DateFormat dateFormat = new SimpleDateFormat(Constants.DBDATEFORMAT);
String fromDateInString = dateFormat.format(MyEntity.getFromDate());
String toDateInString = dateFormat.format(MyEntity.getToDate());
String[] fromDateSplitation = fromDateInString.split(" ");
String[] toDateSplitation = toDateInString.split(" ");
StringBuilder startLimit = new StringBuilder();
StringBuilder endLimit = new StringBuilder();
startLimit.append(fromDateSplitation[0]).append(" ").append("00:00:00");
endLimit.append(toDateSplitation[0]).append(" ").append("23:59:59");
Date fromDate;
try {
fromDate = dateFormat.parse(startLimit.toString());
Date toDate = dateFormat.parse(endLimit.toString());
MyEntity.setFromDate(fromDate);
MyEntity.setToDate(toDate);
predicate.getExpressions().add(criteriaBuilder.between(root.get("toDate"),
MyEntity.getFromDate(), MyEntity.getToDate()));
} catch (ParseException e) {
e.printStackTrace();
}
Can anyone tell me how to properly use Between and Equal for date with criteria builder ?
I've always used your first approach, but not specifying the generic on the Path. Could you maybe try:
.add(criteriaBuilder.between(root.get("toDate"),
MyEntity.getFromDate(),MyEntity.getToDate()));
and most importantly, are you certain that the toDate field of your Entity is Annotated with #Temporal(TemporalType.TIMESTAMP)
what am i doing wrong here, currently xml node in request file coming in, the value 01/22/2020 had to be populated.testers are running new test cases and not populating and it throws an exception.
how can i work to accept empty string.
ParseException: Unparseable date:"".
at java.text.DateFormat.parse
if(stringDate!=null)
You need to add a check for empty also. If it is empty then formatter will not be able to parse it.
if (stringDate != null && !stringDate.isEmpty()) {
if (stringDate.contains("-")) {
format = "yyyy-MM-dd";
} else if (stringDate.contains("/")) {
format = "MM/dd/yyyy";
}
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.parse(stringDate);
} else {
return null;
}
instead of if(stringDate!=null) use StringUtils.isEmpty()
I have below method in which different date patterns have been handled
below is the method in which different date formats have been handled now
now for the particulat format YYYY-MM-dd i don't want it to go for the check where we are prefixing 20 before in code please advise how can i skip that part lets say if the date pattern is YYYY-MM-dd then avoid the logic of prefixing 20 in front of year
below is my code
public java.util.Date extractDate(String dateStr, String dateType) {
String[] datePatternsOfUk = { "d-M-yy", "d-M-yyyy", "d/M/yy", "d/M/yyyy", "yyyy-MM-dd","dd-MM-yy", "dd-MMM-yy","dd-MMM-yyyy","dd-MM-yyyy",
"dd/MM/yy","dd/MMM/yy","dd/MMM/yyyy"};
String[] datePatternsOfUs = { "M-d-yy","MM-dd-yy","M/d/yy","MM/dd/yy", "MM/dd/yy", "MMM-dd-yy",
"MMM/dd/yy", "MMM-dd-yyyy", "MM-dd-yyyy", "MMM/dd/yyyy",
"MM/dd/yyyy" };
java.util.Date date = null;
String[] datePatterns = datePatternsOfUk;
if (dateType.equals("US")) {
datePatterns = datePatternsOfUs;
} else if (dateType.equals("UK")) {
datePatterns = datePatternsOfUk;
}
///******code should not go in this check where date pattern is YYYY-MM-dd
int p = dateStr.lastIndexOf("/");
if (p == -1) {
p = dateStr.lastIndexOf("-");
}
String firstSubstring = dateStr.substring(0, p + 1);
String secondSubstring = dateStr.substring(p + 1);
if (p != -1 && secondSubstring.length() <= 2) {
secondSubstring = Integer.toString(2000 + Integer.parseInt(secondSubstring));
dateStr = firstSubstring + secondSubstring;
}
///****************************************//
try {
date = DateUtils.parseDate(dateStr, datePatterns);
} catch (ParseException ex) {
logger.error("##$$$$$### Error in invoice inside extractDate method : ##$$$$$$#### "
+ ErrorUtility.getStackTraceForException(ex));
}
return date;
}
You could avoid trying any inappropriate pattern by checking if the string "looks like" the pattern before parsing with the pattern.
The general way to do this is:
String datePattern = "yyyy-MM-dd"; // for example
String input;
if (input.matches(datePattern.replaceAll("\\w", "\\d"))) {
// the input looks like the pattern
// in this example "dddd-dd-dd" where "d" is any digit
// so go ahead and try the parse
}
You can enhance this logic to add:
if (input.matches("\\d\\d\\D.*")) {
// then it only has a two digit year, so add "20" to the front
}
if (!dateStr.equals("YYYY-MM-dd")) {
// code
}
converting this nested iteration is blowing my mind could someone convert this to a parallel stream? I want to be able to check for assignments that match my criteria for each of my account team members. I've just switched to java 8 and am struggling to wrap my head around the best way to leverage parallel streams to preform this aggregation.
for(Iterator<BasicDBObject> iterator = members.iterator(); iterator.hasNext();) {
BasicDBObject member = iterator.next();
//if the member doesn't have a valid assignment remove them.
boolean memberContainsValidAssignment = false;
BasicDBObject role = member.get("accountTeamRole") == null ? null : (BasicDBObject) member.get("accountTeamRole");
if (accTeamRoleCodes != null && !accTeamRoleCodes.contains(role.get("code"))) {
iterator.remove();
continue;
}
List<BasicDBObject> assignments = member.get("assignments") == null ? new ArrayList<>() : (List) member.get("assignments");
for (Iterator<BasicDBObject> assignIterator = assignments.iterator(); assignIterator.hasNext(); ) {
BasicDBObject assignment = assignIterator.next();
Date endDate = (Date) assignment.get("assignmentValidToDate");
Date startDate = (Date) assignment.get("assignmentValidFromDate");
if(startDate == null){//this is junk, should have never been allowed.
LOGGER.warn("There's no start date for this assignment. {}", assignment.toString());
assignIterator.remove();
continue;
}
//1. open ended assignment, it lives on forever. 2.falls in between active date.
else if(endDate == null ||(activeDate.after(startDate) && activeDate.before(endDate))){
LOGGER.debug("adding an assignment. {}", assignment.toString());
memberContainsValidAssignment = true;
convertDatesToString(assignment);
continue;
}
}
if(!memberContainsValidAssignment){
iterator.remove();
}
}
Here's a shot at converting your code to use streams. Please, check the details of the filter conditions.
List<BasicDBObject> invalidAssignments = members.stream()
.filter(member -> accTeamRoleCodes == null ||
member.get("accountTeamRole") == null ||
accTeamRoleCodes.contains(member.get("accountTeamRole").get("code"))
)
.flatMap(member -> member.get("assignments").stream())
.filter(assignment -> (Date) assignment.get("assignmentValidFromDate") != null &&
((Date) assignment.get("assignmentValidToDate") != null ||
(activeDate.after((Date)assignment.get("assignmentValidFromDate")) &&
activeDate.before((Date)assignment.get("assignmentValidToDate"))))
)
.collect(Collectors.toList());
To make things parallel would just be a matter of using members.parallelStream instead of members.stream.
I am writing a credit card program. I want the program to use the current date every time the method is used to make a purchase and put the date into the array
private GregorianCalendar transDate;
public CreditCard(double amount,String storeName, GregorianCalendar transDate) {
this.amount=amount;
this.storeName=storeName;
transDate=new GregorianCalendar();
}
public void purchase(double amount, String storeName, GregorianCalendar date)throws Exception
{
if (numPurchases<purchases.length)
if (amount >0 )
if(amount+balance<=creditLimit)
if( GregorianCalendar.getInstance().getTimeInMillis()<=expDate.getTimeInMillis())
{
balance+=amount;
transDate=getTransDate();
purchases[numPurchases] = new CreditCard(amount, storeName,transDate);
numPurchases++;
}
else
{
throw new Exception("card expired");
}
else{
throw new Exception("insufficient credit");
}
else{
throw new Exception("invalid amount");
}
else{
throw new Exception("exceeded number of allowed purchases");
}
}
I would like to display the information in String info
info+="Purchases:\n";
for(int index=0;index<numPurchases;index++){
info+="["+(index+1)+"] ";
info+=transDate.get(Calendar.YEAR)+"\t";
info+= purchases[index].getStoreName()+"\t";
info+=(formatter.format(purchases[index].getPurchase()))+"\n" ;
}
how do I need to set up the code to use the current date and add it to the array and display it in the string
Why don't you use a List implementation instead of an array? You can override the toString method to print it the way you want.
final SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy");
List<Calendar> dates = new ArrayList<Calendar>() {
private static final long serialVersionUID = -5079502477457556887L;
#Override
public String toString() {
Iterator<Calendar> i = iterator();
if (!i.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
Calendar c = i.next();
sb.append(formatter.format(c.getTime()));
if (! i.hasNext())
return sb.append(']').toString();
sb.append(", ");
}
}
};
dates.add(Calendar.getInstance());
dates.add(Calendar.getInstance());
System.out.println(dates);
What does your getTransDate() function do? Ideally it should return the transDate variable of CreditCard object. To calculate transDate for a purchase, you are better off renaming the method to calculateTransDate() or something like that.
Once you have getTransDate() method returning the transDate, your info string can be :
info+="Purchases:\n";
for(int index=0;index<numPurchases;index++){
info+="["+(index+1)+"] ";
info+=purchases[index].getTransDate().get(Calendar.YEAR)+"\t";
info+= purchases[index].getStoreName()+"\t";
info+=(formatter.format(purchases[index].getPurchase()))+"\n"
}