小编naz*_*art的帖子

如何将时间戳字符串更改为“DD.MM.YYYY”?

我需要不同格式的字符串来转换为“DD.MM.YYYY”。

"Thu, 3 Nov 2022 06:00:00 +0100"必须改为"03.11.2022"

"01.11.2022 20:00:00""01.11.2022"

所有格式均为String.

我尝试做

String pattern="DD.MM.YYYY";
DateTimeFormatter formatter=DateTimeFormatter.ofPattern(pattern);

new SimpleDateFormat(pattern).parse("01.11.2022 20:00:00")
Run Code Online (Sandbox Code Playgroud)

我也尝试过执行以下操作

java.time.LocalDateTime.parse(
        item.getStartdatum(),
        DateTimeFormatter.ofPattern( "DDMMYYYY" )
    ).format(
        DateTimeFormatter.ofPattern("DD.MM.YYYY")
    )
Run Code Online (Sandbox Code Playgroud)

但出现错误:

Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Sun, 30 Oct 2022 00:30:00 +0200' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)

我也尝试执行以下操作

Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Sun, 30 Oct 2022 00:30:00 +0200' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)

但是,我没有得到正确的输出。我怎样才能得到我想要的结果?

java string datetime date

2
推荐指数
2
解决办法
948
查看次数

Java:多线程程序中的java.util.NoSuchElementException?

我需要在文件中组织多线程查找:

用户输入find(路径)和find(word);

第一个线程在文件夹中找到.txt文件并将结果添加到队列中; 当队列有一些文件=>第二个线程开始在这个文件中找到需要查找的内容(word).如果找到成功将显示此文件的路径+此文字在文件中遇到的次数.
但是我有NoSuchElementException.

输出:

   Exception in thread "pool-1-thread-2" java.util.NoSuchElementException  
    at java.util.AbstractQueue.remove(AbstractQueue.java:117)  
    at task.FileScan.run(FileScan.java:77)  
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
    at java.lang.Thread.run(Thread.java:722).  
Run Code Online (Sandbox Code Playgroud)

码:

import java.util.concurrent.*;
import java.util.*;
import java.io.*;

class FolderScan implements Runnable {

    private String path;
    private BlockingQueue<File> queue;
    private CountDownLatch latch;
    private File endOfWorkFile;

    FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch,
            File endOfWorkFile) {
        this.path = path;
        this.queue = queue;
        this.latch = latch;
        this.endOfWorkFile = endOfWorkFile;
    }

    public FolderScan() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void …
Run Code Online (Sandbox Code Playgroud)

java multithreading exception

1
推荐指数
1
解决办法
1637
查看次数

其他替代方案使得rand.nextDouble()beetwen -1和1

这个简单的程序程序通过模拟投掷到正方形上的投掷来计算pi的估计.

Сonditions:生成一个随机浮点数并对其进行转换,使其介于-1和1之间.
存储在x中.重复y.检查(x,y)是否在单位圆中,即(0,0)和(x,y)之间的距离<= 1.

在此之后,需要找到与ratio hits / tries比例大致相同的值circle area / square area = pi / 4.(方形是每1 1).

码:

public class MonteCarlo {
    public static void main(String[] args) 
    {
        System.out.println("Number of tries");
        Random generator = new Random(42);
        Scanner in = new Scanner(System.in);
        int tries = in.nextInt();

        int hits = 0;
        double x, y;
        for (int i = 1; i <= tries; i++)
        {
            // Generate two random numbers between -1 and 1            
            int plusOrMinus = generator.nextInt(1000); …
Run Code Online (Sandbox Code Playgroud)

java random

1
推荐指数
1
解决办法
244
查看次数

数字格式异常 - 对于输入字符串:"35,9"

我需要生成随机值并打印它们.

但它引发了一个异常:

 java.lang.NumberFormatException: For input string: "35,9"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.parseDouble(Double.java:540)
    at com.epam.lab.model.sweets.SweetsGenerator.paramFormatter(SweetsGenerator.java:71)
    at com.epam.lab.model.sweets.SweetsGenerator.next(SweetsGenerator.java:55)
    at com.epam.lab.model.NewYearGift.generate(NewYearGift.java:40)
    at com.epam.lab.controller.GiftController.generateGift(GiftController.java:86)
    at com.epam.lab.controller.GiftController.showGiftContent(GiftController.java:213)
    at com.epam.lab.view.Application.process(Application.java:89)
    at com.epam.lab.view.Application.estimateUserInput(Application.java:49)
    at com.epam.lab.view.Application.start(Application.java:43)
    at com.epam.lab.view.Main.main(Main.java:19)
Run Code Online (Sandbox Code Playgroud)

它只发生在这里:

public Sweets next() {      
    Sweets current = instances[rand.nextInt(instances.length)];
    double sugarParam = paramFormatter(randomSugarLevel(), PRECISION);
    double weightParam = paramFormatter(randomWeight(), PRECISION);

    try {
        return (Sweets) current.getClass()
                .getConstructor(double.class, double.class)
                .newInstance(sugarParam, weightParam);
        // Report programmer errors at run time:
    } catch (Exception e) {
        LOG.error("RuntimeException", e);
        throw new RuntimeException(e);
    }
}


private double paramFormatter(double sugarParam, …
Run Code Online (Sandbox Code Playgroud)

java numberformatexception

1
推荐指数
1
解决办法
2105
查看次数

警告:不安全的世界可写的目录

安装Rails后,根据这篇文章Ubuntu,Ruby,RVM,Rails和You.

在使用时scaffold我看到一些奇怪的警告.

我按照下面的步骤:

nazar@lelyak-desktop:~/Documents/rails? rails new blog
nazar@lelyak-desktop:~/Documents/rails/blog? cd blog
nazar@lelyak-desktop:~/Documents/rails/blog? rails generate scaffold post title:string body:text
/home/nazar/.rvm/gems/ruby-2.1.0@global/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /home/nazar in PATH, mode 040777
/home/nazar/.rvm/gems/ruby-2.1.0@global/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /home/nazar in PATH, mode 040777
/home/nazar/.rvm/gems/ruby-2.1.0@global/gems/bundler-1.6.2/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /home/nazar in PATH, mode 040777
/home/nazar/.rvm/gems/ruby-2.1.0/gems/spring-1.1.2/lib/spring/application.rb:164: warning: Insecure world writable dir /home/nazar in PATH, mode 040777
Run Code Online (Sandbox Code Playgroud)

我想知道,如果我理解正确rails使用SqlLite默认情况下?怎么把在blog/db它没有创建任何sqlite3文件?

这里有问题吗?

ruby warnings ruby-on-rails ubuntu-10.04

1
推荐指数
1
解决办法
3791
查看次数

如何将Intellij项目链接到SVN Repo?

我已通过撤消SVN项目Tortoise SVN。将其导入到Intellij Idea。

但是我不能使用IDE来进行更改。

我想知道如何将这个项目链接到远程存储库。并使用快捷方式来提交和提取更改

现在我只能用乌龟做它。

现在的样子:

在此处输入图片说明

我想看的是:

在此处输入图片说明

svn intellij-idea

1
推荐指数
1
解决办法
1019
查看次数

Spring中的FileNotFoundException

我无法解决问题.

完整的堆栈跟踪:

12:14:08.172 [main] DEBUG o.s.core.env.StandardEnvironment - Adding    [systemProperties] PropertySource with lowest search precedence
12:14:08.178 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
12:14:08.178 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
12:14:08.183 [main] INFO  o.s.c.s.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24273305: startup date [Thu Mar 16 12:14:08 GMT 2017]; root of context hierarchy
12:14:08.222 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
12:14:08.223 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
12:14:08.223 …
Run Code Online (Sandbox Code Playgroud)

java spring maven-2 derby

1
推荐指数
1
解决办法
6368
查看次数

Spring boot @Scheduled(cron) 每 2 小时一次

我想每 2 小时触发一次我的函数。

您能提供一下@Scheduled这个案例的表述吗?

例子:

@Scheduled(cron = "0 0 0 * * ?", zone = "亚洲/加尔各答")

spring spring-boot

1
推荐指数
1
解决办法
2万
查看次数

计算日期范围匹配到 Java

我想实现此代码以检查用户是否订阅了服务:

public String calculateSubscription(String email) {

    Optional<Subscription> subscriptionsByUserEmail = subscriptionService.findSubscriptionsByUserEmail(email);
    if (subscriptionsByUserEmail.isPresent()) {
      Subscription subscription = subscriptionsByUserEmail.get();

      LocalDateTime startAt = subscription.getStartAt();
      LocalDateTime endAt = subscription.getEndAt();
      LocalDateTime now = LocalDateTime.now();

      if(/* do here comparison */){
        return "subscribed";
      }
    }
    return "unsubscribed";
  }
Run Code Online (Sandbox Code Playgroud)

到数据库中,我存储了 2 个字段:

@Column(name = "start_at")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime startAt;

@Column(name = "end_at")
@Convert(converter = LocalDateTimeConverter.class)
private LocalDateTime endAt;
Run Code Online (Sandbox Code Playgroud)

我需要检查now时间间隔startAtendAt.

这个检查如何实现到 if 语句中?

编辑:

@Converter(autoApply = true)
public class LocalDateTimeConverter implements …
Run Code Online (Sandbox Code Playgroud)

java date-range localdatetime

1
推荐指数
1
解决办法
115
查看次数

Set 的 addAll 抛出 java.lang.UnsupportedOperationException

使用JDK 11.0.3。我有以下代码片段:

Set<String> allNumbersSet = customerInfoService.getCustomerPhoneNumbers(bankCustomerId);
additionalInformation
        .map(info -> info.get(BANK_PE_CUSTOMER_ID_KEY))
        .filter(StringUtils::isNotEmpty)
        .ifPresent(id -> allNumbersSet.addAll(customerInfoService.getCustomerPhoneNumbers(id))); // fails here
Run Code Online (Sandbox Code Playgroud)

获取电话号码的地方就是Collectors.toSet()

@Override
public Set<String> getCustomerPhoneNumbers(String customerId) {
    return backOfficeInfoClient.getCustByHashNo(customerId).getPropertyLOVs()
            .flatMap(property -> property.getValues().values().stream())
            .collect(Collectors.toSet());
}
Run Code Online (Sandbox Code Playgroud)

但是,它失败了:

java.lang.UnsupportedOperationException
    at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
    at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.addAll(ImmutableCollections.java:76)
    at service.impl.UserManagementServiceImpl.lambda$validateNewLogin$3(UserManagementServiceImpl.java:69)
Run Code Online (Sandbox Code Playgroud)

如果我像下面这样更新:

var allNumbersSet = new HashSet<>(customerInfoService.getCustomerPhoneNumbers(bankCustomerId));
Run Code Online (Sandbox Code Playgroud)

现在效果很好。

上面的代码使用有什么问题吗?您能解释一下为什么会出现这种情况吗?


此方法调用由调用 Hazelcast 缓存包围 - 之前和之后。正如评论中提到的,这可能是这种行为的原因:

缓存的值使用不可变集合表示,这是有道理的,因为这允许共享而无需防御性副本

解决方案:

找到了如何重写此逻辑并在不合并两组的情况下执行该操作的方法:

var numbersSet = customerInfoService.getCustomerPhoneNumbers(id);
if (!numbersSet.contains(newLogin)) {
    var peNumbersSet = additionalInformation
            .map(info -> info.get(BANK_PE_CUSTOMER_ID_KEY))
            .filter(StringUtils::isNotEmpty)
            .map(customerInfoService::getCustomerPhoneNumbers)
            .orElseGet(Collections::emptySet);

    if (!peNumbersSet.contains(newLogin)) {
        throw …
Run Code Online (Sandbox Code Playgroud)

java exception set unsupportedoperation java-11

1
推荐指数
1
解决办法
1228
查看次数